My problems here is that they are loading the data from an external resource which return JSON, instead of this I want to know how can we give our own internal JSON data which is there in a variable as a URL parameter
Related
If i want to isolate a fetch request that returns a json in a separate file i should do?
opt 1.
Separate only the fetch request and return the response object, then in the call this function do response.json()
opt2
separate request and after run response.json() method and return the response.json from the function
Follow your second approach.
Best practice in every case to bundle your requirement into same function. So here - FETCH REQUEST, GET RESPONSE, CONVERT RESPONSE AS REQUIRED, RETURN RESPONSE must be in the same method.
For the part of the code that requires the data, it is in most cases not of interest how this data is retrieved (if it is requested from the server using fetch, using WebSocket, if it is cached in local storage, ...), so you normally don't want to return an object that is related to the type of transmission, but only the received data or a customer Result object that is not related to the API you use to request the data, but one that you define yourself.
That way you can easily change the transmission type at any time, add caching, offline functionalities, ... , and you don't need to change the parts of the code that is requesting the data.
I have a below scenario:
During run time I want to take whole request and place in response
Request is in XML format
I want to take that request and store it in a variable either it is in string or XML also fine
4.It should be done with Java script
I am able to take the request and convert it to JSON with JSON.Stringify function. In the same way is there any function to read directly as XML or converting XML to String and store it in a variable?
Is there any way I can query the image from SalesForce server as a blob object ?
We already have forcetk client queries which is retrieving all the data, but the image alone is returned as a link (salesforce link).
Can we retrieve image as blob in the same REST query call ?
The methods I saw required to make an extra call to fetch images, but here I have images in each row of the result, it would have been better if images come as a part of result object itself.
If you're using the REST API, then the blob data can't be returned with the rest of the object, it has to be a separate request. If you use SOAP then it can inline the blob data with the rest of the fields for the object, but it will be limited to returning one row at a time.
i'm using phonegap in order to make an application, then i'm using CodeIgniter as an API.
To get the data, i'm using a simple $.post from jQuery to a REST URL in CodeIgniter.
$.post(DIR+trad+"/format/json", function(data){
traducciones = jQuery.parseJSON(data);
}, json);
Based upon this, i got 2 questions, because i don't know if i should do it in different post methods.
The first question is, how do i get the data in json format?
The second question is, is there any way to call the json object directly from my view (HTML) ? or i need to do it using jquery or javascript?
how do i get the data in json format?
Searching for codeigniter+json gives me Output Class which has this example:
$this->output
->set_content_type('application/json')
->set_output(json_encode(array('foo' => 'bar')));
is there any way to call the json object directly from my view (HTML)?
Generally speaking, if you are generating the JSON yourself and you want to use it in HTML, then you will just skip generating JSON and use the data directly.
Generating JSON from a view only really becomes useful if you want to make the raw data available over a network.
If you want to fetch the data over HTTP for your view, then that is a job for the Model. See How to send a GET request from PHP?. This is useful if you have two different applications. One providing a web service for the data and one providing the user facing application.
If you want to update the page with new data after it has loaded, then this is a good usecase for JSON, but you can only do that with JavaScript (or another client side programming language).
If you want to using JSON as response format, just do like
$.post(url, {
"dataType" : "json",
"success" : function(response) {
// process your response here, it will be treated as JSON object
}
});
Am I allowed to make an API call directly from Javascript to an external API ( in my case http://isbndb.com/docs/api/20-structure.html ) using the XMLHttpRequest object? If not how do I go about fetching data from this source? Do I have to add a PHP back end which makes the API call and then returns data to the front end JS? Is there any other way to fetch data without the need for back end code? I did some research online and found that this is possible in jQuery if the data format is JSON. But how do I do this with XML data?
If you are using jquery's ajax method, you can pass it as a parameter a dataType, it can be json, xml, html or script. When it is set to xml in your success callback's first parameter will be XML from the server as a XML document you can work with