How to convert XML(SOAP) to JSON object by JavaScript - javascript

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

Related

How can I send YAML over AJAX?

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

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.)

Jaggery JS Convert WSRequest to JSON

I am calling a data service from a jaggery js app using http://jaggeryjs.org/apidocs/ws.jag . The data comes back in XML with members .responseText and .responseE4X. Is there an easy way to convert this response to JSON? Is there a parameter I can set to have this respond with JSON? This is being run in the server side, so I do not have access to window(), which most XML to JSON converters use.
Thanks!
I did this by using the post method: http://jaggeryjs.org/apidocs/post.jag
It is worth noting that there is a bug in this as well. I had to set the content type header as "content-type" and not "Content-Type".

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 + ')');

XML for Ajax PUT or POST request

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 &

Categories