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 + ')');
Related
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.)
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
}
});
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.
is this possible to send cross site request by AJAX with a SOAP request and get XML response?
and i want to convert my xml response to json format is there any framework (like mustache) to do this easily
You can make use of xml2js node library. It converts xml to json and vice versa. But it doesn't uses templates. https://www.npmjs.com/package/xml2js
I'm looking to write some Javascript which will make an Ajax PUT or POST request to an HTTP server. I'm assuming that the information which gets passed as the argument to request.send needs to be in XML format. Could somebody shoe me an example of how to create this XML and pass it to request.send([Entity-body]) as the entity-body.
Thanks!
The "xml" in XmlHttpRequest is entirely superfluous; there's never been a requirement that the request or the response is in xml format.
In fact, it's just as common to send JSON (JavaScript object notation) instead of xml.
So, don't use xml unless you want to. Just send any string you want.
You don't have to send xml, you can send any arbitrary string.
an example for http post would be
request.send("id=1&somattribute=value&etc=etcetc");
where you have name value pairs
name=value
separated by &