跨域AJAX请求返回字符串
我正在寻找一种从跨域“AJAX”请求返回单个 JSON/JSONP 字符串的方法。我不想请求字符串并让 JQuery 自动将其作为通用对象返回,而是想在转换发生之前获取字符串。这里的目标是自己解析它,这样我就可以将它直接转换为某种类型的新对象(例如 Person 对象)。
因此,为了明确这一点,我不希望在幕后进行任何字符串到通用对象的转换,并且这必须使用不同的域来工作。
这是我想做的一个非工作示例:
$.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
}
如果 JQuery 不参与解决方案,只要它能工作,我就非常高兴。不过,我更喜欢使用 JQuery。根据我所读到的内容,用于获取 JSONP 数据(动态创建脚本元素)的 javascript 技术可能会起作用,但我似乎无法让它为我工作。我控制从中请求数据的域,如果我将 AJAX 调用中的数据类型更改为“JSONP”,我就可以获得数据,所以我知道这是有效的。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您的数据是从另一个域检索的,您将需要使用 JSONP(还有其他选项,但如果您控制服务,JSONP 是迄今为止最简单的)。 jQuery 调用将如下所示:
发送到您的服务的实际请求将是
http://www.someOtherDomain.com/GetPerson?callback=atory_function_name
。在服务端,您需要返回如下数据:因此,您需要检查查询字符串参数,获取
callback
参数的值,然后将其回显,就像您正在调用一样一个具有该名称(即您的名称)的 Javascript 函数,传入您想要通过服务提供的值。然后,您的success
函数将使用您的服务提供的数据进行调用。如果您将返回的数据反序列化为 Javascript 对象,那么返回 JSON 数据可能比返回字符串更好,因此您的服务返回的数据可能如下所示:
这样您就不必担心解析字符串 -它已经在一个 Javascript 对象中,可以很容易地用来初始化你的对象。
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:
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: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. Yoursuccess
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:
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.
不确定这将如何与 jsonp 结合使用,但也许 converters 就是你的正在寻找?
Not sure how this will work in conjuction with jsonp, but maybe converters is what you're looking for?