简体   繁体   中英

Return String from Cross-domain AJAX Request

I'm looking for a way to return a single JSON/JSONP string from a cross-domain "AJAX" request. Rather than request the string and have JQuery return it as a generic object automatically, I want to get a hold of the string BEFORE that conversion happens. The goal here is to parse it myself so I can turn it straight into new objects of a certain type (eg a Person object).

So, just to make this clear, I don't want any string-to-generic-object conversion going on behind the scenes and this must 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'm perfectly happy if JQuery isn't involved in the solution, as long as it works. I would prefer to use JQuery, though. From what I've read, the javascript techniques used to get JSONP data (dynamically creating a script element) would probably work, but I can't seem to get that to work for me. I control the domain that I am requesting data from and I can get the data if I change the dataType in the AJAX call to 'JSONP', so I know that is working.

If your data is being retrieved 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). The 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 goes to your service will be http://www.someOtherDomain.com/GetPerson?callback=arbitrary_function_name . On the service side, you will need to return data like this:

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

So you'll need to inspect the querystring parameters, get the value of the callback parameter, and echo it out as if you're calling a Javascript function with that name (which you are), passing in the value you want to provide through the service. Your success function will then get called with the data your service provided.

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

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

That way you don't have to worry about parsing the string - it'll already be in a Javascript object that's easy to use to initialize your object.

Not sure how this will work in conjuction with jsonp, but maybe converters is what you're looking for?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM