开发者

Wiring up JSONP using JQuery and WCF

I'm trying to get a cross domain call to w开发者_Go百科ork using JSONP within JQuery. In IE, the alert method never executed. In FF/Safari/Chrome, it's always null. I looked at Fiddler and the result from the WCF method is as I'm expecting, which is:

method({"Name":"blah1","Data":"blah2"});

Here's my JavaScript:

$.getJSON("http://localhost:5603/MyService/?method=test", null, function (result) {
    alert("in test: " + result);
    $("#spText").html(result);
});

Here's the WCF method:

[OperationContract]
[WebInvoke(UriTemplate = "", Method = "GET", 
    BodyStyle=WebMessageBodyStyle.Bare,
    ResponseFormat = WebMessageFormat.Json)]
public Message Blah()
{
    var j = new { Name = "blah1", Data = "blah2" };

    JavaScriptSerializer s = new JavaScriptSerializer();
    string jsonClient = s.Serialize(j);

    return WebOperationContext.Current.CreateTextResponse("method(" + jsonClient + ");",
        "application/json; charset=utf-8", Encoding.UTF8);
}

I feel like I'm really close on this. Can anyone spot anything I'm doing wrong?


Try changing

http://localhost:5603/MyService/?method=test

to

http://localhost:5603/MyService/?method=test&callback=?

From the documentation:

If the URL includes the string "callback=?" in the URL, the request is treated as JSONP

References: http://api.jquery.com/jQuery.getJSON/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜