Making an external API call from Javascript. - javascript

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

Related

How to return JSON data from an Ajax call NO JQUERY

I am making calls to my server with Ajax. The data is returned and sent to a callback method. I want to have the response data formatted in data. How can I do this? I'm assuming it involves XML -> JSON conversion. I do not want to use the jQuery ajax method.
jQuery will not have anything to do with the process you must perform server side. Regardless of your server side technology, you will need to convert the result into the JSON format.
The process of conversion is known as "serialization"1. Use a serializer to convert your server side object into JSON format so that you may use it again when it is returned to your client side script.
1. Serialization - Wikipedia, the free encyclopedia
2 solutions :
put it in a global variable or use a callback
You cannot return a xmlhttp response (tried few days ago, used jQuery it saved my life.)

use ajax returned value in php

in my js script tag I hv a var that store data of my ajax success callback. let say it's $result. How can I use it in php? I don't want to directly use for example $('#element').html($result) to manipulate the DOM.
You don't need to send the response to browser instead of that you can use the request and manipulate as you want it in server side that is in php. It is not necessary to send the response back to client.
You should manipulate/Filter the result before getting it from ajax and then use it to manipulate DOM/or for whatever purpose you like.. I guess this is the best option.... Else it'll raise the iteration time and page load time.
So just manipulate it before returning from the ajax file
and if you wish to use it in php which will load it again then why to use ajax.

json object directly from my view HTML

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
}
});

Request JSON object from server?

i have a JSON object in server side:
String jsonText = JSONValue.toJSONString(users);
and i want to send it to client side to be used in a javascript function, how to do that ?
I am using JSF 2 with Pure JavaScript with no other libraries.
In HTTP, the server doesnt send anything to client-side. The client-side asks for some resource using a request, and the response contains the resource.
Make the JavaScript function send an AJAX request to the server, and make the server answer with this JSON string in the response (by writing the JSON String to the response writer).
See http://api.jquery.com/jQuery.getJSON/ to get a JSON String from JavaScript. The server code is as simple as
response.getWriter().print(jsonText);
The easiest way is to let JSF print the string representation of the JSON object as a JS variable in the same view:
<script>var users = #{bean.usersAsJson};</script>
The correct way, however, is to use a fullworthy JSON web service for this. JSF is a component based MVC framework, not a JSON web service. For that the Java EE stack offers the JAX-RS API. Long answer short: Servlet vs RESTful. Use this preferably in combination with jQuery or whatever JS framework which is able to send Ajax requests and manipulate the HTML DOM tree in basically an oneliner. You only need to take into account that when used in combination with JSF, this can be used for pure presentation only.
The browser must request it, ie that string must sit somewhere in a request handling chain; set the response to that string and the browser will receive it.
I think you wanted to say response the json string.
It can be done by jQuery function getJSON.
It will get the json string and parse it inti hash, then you have to process it for your needs.
API documentation
send your json object to response and get the response in the javascript.
use eval function to evaluate json object.
var jsonObject = eval('(' + request.responseText + ')');

Is it possible to parse JSON with javascript?

I have an external URL for a JSON file which is hosted on another domain (not mine). Is it possible to parse this information with javascript only? Here is a sample of the JSON data. I only want to get "q" values.
[{"url":"http://website.com/?q=who+is+ip+search","q":"who is ip search"},{"url":"http://website.com/?q=eclipse+visual+editor","q":"eclipse visual editor"},{"url":"http://website.com/?q=partition+recovery","q":"partition recovery"},{"url":"http://www.website.com/?q=katzenfurz","q":"katzenfurz"},{"url":"http://website.com/?q=rtfm","q":"rtfm"},{"url":"http://website.com/?q=Google+ist+Dein+Freund","q":"Google ist Dein Freund"}]
Browsers have native parsing methods -> JSON.parse() and JSON.stringify()
There are also several libraries that add the ability to parse JSON ...
http://www.json.org/js.html
http://developer.yahoo.com/yui/json/
http://api.jquery.com/jQuery.parseJSON/
Eval is sometimes used directly within JavaScript - but there are often security concerns when using this method -> http://en.wikipedia.org/wiki/JSON#JavaScript_eval.28.29
Yes, there is a built-in JSON.parse() function. Just pass the string to the function.
var obj = JSON.parse( data );
Live demo: http://jsfiddle.net/h4XTP/
JSON you know is JavaScript object; yes you can parse it in JS. Though as you have remote server as data publisher, you have to configure that server for a callback function.
To make the remote request, you insert a new script tag into your page, which will allow you to specify a remote URL. The reponse back will load a JSON object as a parameter of the callback function you specified in the request.
Once read somewhere. Hope it helped.

Categories