返回的字符串的跨域AJAX请求 [英] Return String from Cross-domain AJAX Request

查看:101
本文介绍了返回的字符串的跨域AJAX请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在寻找一种方式来从跨域Ajax的请求返回一个JSON / JSONP字符串。而不是请求字符串并jQuery将自动返回它作为一个通用的对象,我希望得到一个持有字符串之前的转换发生。这里的目标是分析它自己,所以我可以把它直接进入某一类型的新对象(例如Person对象)。

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
}

我完全高兴,如果jQuery是不参与的解决方案,只要它的作品。我想preFER使用jQuery,虽然。从我读过,JavaScript的技术用于获取JSONP数据(动态创建脚本元素)可能会工作,但我似乎无法得到这为我工作。我的控制,我的请求数据,我可以得到的数据,如果我更改AJAX调用'JSONP具体的数据类型的域,所以我知道是工作。

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.

推荐答案

如果您的数据从另一个域恢复,您将需要使用JSONP(还有其他的选择,但JSONP是目前为止最简单的,如果你控制服务)。 jQuery的通话将是这样的:

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

这是去你的服务的实际要求将 http://www.someOtherDomain.com/GetPerson?callback=arbitrary_function_name 。在服务方面,你将需要返回的数据是这样的:

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

所以你需要检查查询字符串参数,得到了回调参数的值,并呼应它,如果你调用一个JavaScript函数与名字(你的),传递你想通过提供服务的价值。你的成功函数将被调用所提供服务的数据。

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.

如果您要反序列化返回的数据转换为JavaScript对象,你可能会更好比返回一个字符串JSON数据,所以数据的服务回报可能是这样的:

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

这样,你就不必担心解析字符串 - 它会已经在一个Javascript对象,易于使用初始化对象

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.

这篇关于返回的字符串的跨域AJAX请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆