jQuery 跨域 Ajax [英] jQuery Cross Domain Ajax

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

问题描述

我的ajax代码是

$.ajax({
    type: 'GET',
    dataType: "jsonp",
    processData: false,
    crossDomain: true,
    jsonp: false,
    url: "http://someotherdomain.com/service.svc",
    success: function (responseData, textStatus, jqXHR) {
        console.log("in");
    },
    error: function (responseData, textStatus, errorThrown) {
        alert('POST failed.');
    }
});

这是一个跨域ajax请求.

This is a cross domain ajax request.

我得到了对请求的正确响应,在使用 firebug 进行检查时,我可以看到该响应.

I am getting correct response for the request, while checking with firebug i can see that response.

这是我在萤火虫响应和通过网络浏览器访问此网址时得到的响应

This is the response I am getting in firebug response and while accessing this url through web browser

{"AuthenticateUserResult":"{"PKPersonId":1234,"Salutation":null,"FirstName":"Miqdad","LastName":"Kumar","Designation":null,"Profile":"","PhotoPath":"/UploadFiles/"}"}

但我收到错误

SyntaxError: invalid label

{"AuthenticateUserResult":"{"PKPersonId":8970,"Salutation

我是否需要使用任何其他方法才能使其正常工作.我想在 phonegap+jquery 移动应用中实现这个.

Whether I need to use any other method to get it works. I want to implement this in phonegap+jquery mobile app.

此外,我无法访问网络服务

Also, I don't have any access to the web service

如果我禁用了 chrome 网络安全,它就可以正常工作

If I disable chrome web security it's working fine

推荐答案

看起来内部 JSON 结构是作为字符串传递的.您必须再次使用 JSON.parse() 才能将该数据作为对象获取.

Looks like the inner JSON struct is passed along as a string. You'll have to JSON.parse() it once more to get that data as an object.

try {
  responseData = JSON.parse(responseData);
}
catch (e) {}

请尝试以下操作:

$.ajax({
    type: 'GET',
    dataType: "json",
    url: "http://someotherdomain.com/service.svc",
    success: function (responseData, textStatus, jqXHR) {
        console.log("in");
        var data = JSON.parse(responseData['AuthenticateUserResult']);
        console.log(data);
    },
    error: function (responseData, textStatus, errorThrown) {
        alert('POST failed.');
    }
});

这篇关于jQuery 跨域 Ajax的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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