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.
source share