jQuery的跨域阿贾克斯 [英] jQuery Cross Domain Ajax

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

问题描述

我的阿贾克斯code是

  $。阿贾克斯({
    键入:GET,
    数据类型:JSONP
    过程数据:假的,
    跨域:真正的,
    JSONP:假的,
    网址:http://someotherdomain.com/service.svc
    成功:函数(responseData,textStatus,jqXHR){
        的console.log(中);
    },
    错误:函数(responseData,textStatus,errorThrown){
        警报(POST失败。');
    }
});
 

这是一个跨域Ajax请求。

我收到了请求正确的反应,一边用萤火虫检查,我可以看到该响应。

这是我收到的萤火响应响应,并同时通过Web浏览器访问该网址

<$p$p><$c$c>{"AuthenticateUserResult":"{\"PKPersonId\":1234,\"Salutation\":null,\"FirstName\":\"Miqdad\",\"LastName\":\"Kumar\",\"Designation\":null,\"Profile\":\"\",\"PhotoPath\":\"\/UploadFiles\/\"}"}

不过,我收到错误

 语法错误:无效的标签

{AuthenticateUserResult:{\PKPersonId \:8970,\称呼\
 

我是否需要使用其他方法来获得它的工作原理。我想在PhoneGap的+ jQuery Mobile的应用程序来实现这一点。

另外,我没有任何访问该Web服务

如果我禁用Chrome网络的安全性它的正常工作

解决方案

看起来像内部JSON结构沿作为字符串传递。你必须JSON.parse(),它再一次得到的数据作为一个对象。

 尝试{
  responseData = JSON.parse(responseData);
}
赶上(五){}
 

编辑: 请尝试以下操作:

  $。阿贾克斯({
    键入:GET,
    数据类型:JSON,
    网址:http://someotherdomain.com/service.svc
    成功:函数(responseData,textStatus,jqXHR){
        的console.log(中);
        VAR数据= JSON.parse(responseData ['AuthenticateUserResult']);
        的console.log(数据);
    },
    错误:函数(responseData,textStatus,errorThrown){
        警报(POST失败。');
    }
});
 

My ajax code is

$.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.');
    }
});

This is a cross domain ajax request.

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\/\"}"}

But I am getting error

SyntaxError: invalid label

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

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

If I disable chrome web security it's working fine

解决方案

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

Edit: Try the following:

$.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的跨域阿贾克斯的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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