使用JQuery和WCF接线JSONP [英] Wiring up JSONP using JQuery and WCF

查看:214
本文介绍了使用JQuery和WCF接线JSONP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在JQuery中使用JSONP获得跨域调用。在IE中,警报方法从未执行过。在FF / Safari / Chrome中,它始终为null。我看着Fiddler,WCF方法的结果是我所期望的,它是:

I'm trying to get a cross domain call to work 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"});

这是我的JavaScript:

Here's my JavaScript:

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

这里是WCF方法:

[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?

推荐答案

尝试更改

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


$ b b

to

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

从文件:


如果网址在网址中包含字符串callback =?,则会将该请求视为JSONP

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

参考文献: http://api.jquery.com / jQuery.getJSON/

这篇关于使用JQuery和WCF接线JSONP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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