使用jquery ajax从跨域进行JSON解析 [英] JSON parsing from cross domain using jquery ajax

查看:120
本文介绍了使用jquery ajax从跨域进行JSON解析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从跨域解析json,但是在jquery插件中出现错误,例如 405(不允许使用方法)(仅使用来自Google的最新插件),任何解决方案或建议都将对我.

Im trying to parse json from cross domain but im getting error like 405 (Method Not Allowed) in jquery plugin (im using latest plugin only from google) Any solution or suggestions will be great help for me.

谢谢 巴莎

这是我的代码

$(document).ready(function() {
    $.ajax({
    type: "GET",
    url: "http://myurl.com/webservice&callback=?",          
    contentType: "application/json; charset=utf-8",
    crossDomain: true,
    dataType: "jsonp",
    data: "{}",
    Accept: "",
    beforeSend: setHeader,
    success: OnGetAllMembersSuccess,
    error: OnGetAllMembersError,                
    });
});     
function setHeader(req) {
    req.setRequestHeader("Authentication", "Basic credentials");
    req.setRequestHeader("Content-Type", "application/json");
    req.setRequestHeader("Accept", "application/json");
}    

function OnGetAllMembersSuccess(data, status) {
    alert(status);
    $.each(data.result, function(key, value) {              
        $("#result").append(key+" : "+value);
        $("#result").append("<br />");
    });
}

function OnGetAllMembersError(request, status, error) {
    alert(status);
}   

推荐答案

使用jsonp作为dataType时,您需要在服务器端绑定一个回调函数. 例如,如果您需要一个像{"id":"myId"}这样的json响应,则在服务器端应该像"mycallback({" id:" myId})";

While using jsonp as dataType, you need to bind a call back function in the server side.. for example, if you need a json response like {"id":"myId"}, at the server side it should be return like "mycallback({"id":"myId"})";

此外,您也需要在客户端编写该函数.

Also you need to write that function in client side too.

function mycallback(json)
{alert(json);}

这篇关于使用jquery ajax从跨域进行JSON解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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