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