我正在尝试在jquery中使用jsonp进行跨域调用。在IE中,警报方法从未执行。在FF/Safari/Chrome中,它始终为空。我看了一下fiddler,wcf方法的结果如我所料,它是:
method({"Name":"blah1","Data":"blah2"});
这是我的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);
}
我觉得我对这件事很感兴趣。有人能发现我做错了什么吗?