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 &
Related
Please focus on the technical aspect of this question, and not on the why. The why is obvious: YAML is the most human-readable data serialization format available to man. And therefore, the best.
How can I send YAML via an XMLHttpRequest from the client to the server, without first converting it to JSON, XML or another format?
I am using JavaScript for the client-side code, I can use jQuery if needed. My server-side language of choice is PHP.
According to Wikipedia, the send() method of XMLHttpRequest:
Accepts a single parameter containing the content to be sent with the request. The W3C draft states that this parameter may be any type available to the scripting language as long as it can be turned into a text string, with the exception of the DOM document object. [Emphasis my own]
YAML is a text string. Can it be sent and subsequently parsed correctly on the server-side without using another data serialization format like json, xml etc?
Just set an appropriate content type and send it.
// xhr is an instance of XMLHttpRequest which has been open()ed and had event handlers set on it already
xhr.setRequestHeader("Content-Type", "text/x-yaml");
xhr.send(string_of_yaml_formatted_data);
Note, that most server side languages won't parse it automatically, so you'll need to read the raw data from the POST request body and parse it yourself.
e.g. in PHP:
$raw_yaml = file_get_contents('php://input');
$data = yaml_parse($raw_yaml);
Note: yaml_parse() requires PECL yaml >= 0.4.0
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 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 + ')');
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.
I would really appreciate If anyone can explain methe following:
document.getElementById("txtHint2").innerHTML=xmlHttp.responseText;
I understand that result (right side after equal sign) will be written within div tag in this case...But what is this RESPONSETEXT actually?
I have html calling some *.js. In this I have url to php. In php is command connect to database and give results after my query.
So result of this query sholud be related to =responseText ...
I am confused and not so familiar about this ...Please help me!!!
you may read;
Using jQuery and JSON with AJAX responseText?
It contains the content from the URL you requested using the XMLHttpRequest object.
The object xmlHttp has the property responseText that is the response of your XML HTTP Request ("ajax" return). It's like when you load a page in a browser and you get your page.
This object has many properties, like:
responseText: Returns the response data as a string
responseXML: Returns the response data as XML data
Referece: http://www.w3schools.com/dom/default.asp.
xmlHttp.responseText is the body of the response from the server. It is whatever was sent back from the server based on the request (specified in the xmlHttp object). It could be a simple string of text, or a snippet of HTML, or JSON to be parsed and used by your code.
the requested url will generate an output(text/html). This output is sent to the client(requested page) through xmlHttp.responseText;
You may also get xml as an output, then you need to use xmlHttp.responseXML; you need to parse this and use it in your application..