Return string from cross-domain AJAX request

I am looking for a way to return a single JSON / JSONP string from a cross-domain "AJAX" request. Instead of asking for a string and jQuery automatically returns it as a shared object, I want to get the string before the conversion happens. The goal here is to independently analyze it, so that I can turn it directly into new objects of a certain type (for example, a Person object).

So, just to make this clear, I don’t want the conversion of strings to a common object to happen behind the scenes, and this should work using a different domain.

Here's a non-working example of what I would like to do:

$.ajax({ type: 'GET', url: 'http://www.someOtherDomain.com/GetPerson', dataType: 'text', success: parseToPerson }); function parseToPerson( textToParse ) { // I think I can do this part, I just want to get it working up to this point } 

I am very happy if jQuery is not involved in the solution while it is working. I would prefer to use jQuery. From what I read, the javascript methods used to get JSONP data (dynamically create a script element) will probably work, but I can't get this to work for me. I manage the domain from which I request data, and I can get the data if I change the data type in the AJAX call to "JSONP", so I know that it works.

+2
source share
2 answers

If your data is extracted from another domain, you will need to use JSONP (there are other options, but JSONP is by far the easiest if you control the service). A jQuery call will look like this:

 $.ajax({ // type: 'GET', --> this is the default, you don't need this line url: 'http://www.someOtherDomain.com/GetPerson', dataType: 'jsonp', success: parseToPerson }); 

The actual request that comes to your service will be http://www.someOtherDomain.com/GetPerson?callback=arbitrary_function_name . On the service side, you will need to return the data as follows:

 arbitrary_function_name("the string (or JSON data) that I want to return"); 

So, you will need to check the request parameters, get the value of the callback parameter and repeat it as if you were calling the Javascript function with that name (which you are), passing the value you want to provide through the service. Then your success function will be called with the data provided by you.

If you deserialize the returned data into a Javascript object, you might be better off returning JSON data than a string, so the data returned by your service might look like this:

 arbitrary_function_name({ "name":"Bob Person", "age":27, "etc":"More data" }); 

This way, you don’t have to worry about parsing the string β€” it will already be a Javascript object that will be easy to use to initialize your object.

+1
source

Not sure how this will work in conjunction with jsonp, but maybe converters are what you are looking for?

 $.ajax(url, { dataType: "person", converters: { "text person": function(textValue) { return parseToPerson(textValue); } } }); 
0
source

Source: https://habr.com/ru/post/890087/


All Articles