Return String from Cross-domain AJAX Request

落花浮王杯 提交于 2019-12-22 18:34:51

问题


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 (e.g. 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.


回答1:


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.




回答2:


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);
    }
  }
});


来源:https://stackoverflow.com/questions/6376623/return-string-from-cross-domain-ajax-request

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!