C# 使用jsonp调用外部站点webmethod?
我正在尝试调用外部站点webmethod,并发布一些数据。我尝试了很多不同的方法,但仍然无法调用该方法 以下是我的js代码:C# 使用jsonp调用外部站点webmethod?,c#,asp.net,json,jsonp,C#,Asp.net,Json,Jsonp,我正在尝试调用外部站点webmethod,并发布一些数据。我尝试了很多不同的方法,但仍然无法调用该方法 以下是我的js代码: $.ajax({ url: "http://sitename.com/methods.aspx/mywebmethod", data: "{'id':'" + 4 + "'}", dataType: "jsonp", type: "GET", conte
$.ajax({
url: "http://sitename.com/methods.aspx/mywebmethod",
data: "{'id':'" + 4 + "'}",
dataType: "jsonp",
type: "GET",
contentType: "application/json; charset=utf-8",
success: function (data) {
alert(data);
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert(errorThrown);
}
});
以下是我的webmethod代码:
[WebMethod()]
public static bool mywebmethod(int id)
{
if(id != 0){
return true;}
else{return false;}
}
我总是得到同样的回答
Error: jQuery{code} was not called
我错过了什么?JSONP不是魔法
您只能使用JSONP从返回的URL读取数据。ASP.Net WebMethods不支持JSONP。我猜您缺少正确的属性,如下所示(在.asmx定义中): 另外,您还需要使用内容重写模块来处理前导回调参数:
你不能。您需要了解JSONP是如何工作的。是的,使用服务器端脚本调用外部Web服务。忘掉javascript吧。为了理解为什么要忘记javascript,遵循SLaks的建议,阅读关于JSONP的内容,以了解它的工作原理,并了解内置于浏览器中的阻止您发送跨域AJAX请求的同源策略限制。在我的情况下,我需要从客户端进行调用。在这种情况下,您必须修改远程服务,使其返回JSONP而不是JSON。
[WebMethod(EnableSession = true)] // optional, but usually forgotten
[ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
public bool MyMethod(int id)
{
return true;
}