我的Ajax调用没有执行web方法,并返回状态代码“302 Found” . 更进一步......检查跟踪显示该方法实际上是作为“OPTIONS”发送的?
当web方法在aspx文件中时,这一切都正常 . 当我将代码转换为UserControl时,我不得不将其移动到asmx .
Ajax调用:
$.ajax({
type: "POST", //HTTP method
url: '<%= ResolveUrl("http://tempuri.org/PLService.asmx/getLocations")%>', //page/method name
data: "{}", //json to represent argument
contentType: "application/json; charset=utf-8",
dataType: "json",
success: processData //handle the callback to handle response
})
PLService中的Web方法
[WebMethod]
public static string getLocations()
{
System.Diagnostics.Debug.WriteLine("Getting Locations.");
return "{\"region\":\"auckland\", \"city\":\"auckland\"}";
}
请求:
主持人:tempuri.org用户代理:Mozilla / 5.0(Windows NT 6.1; WOW64; rv:37.0)Gecko / 20100101 Firefox / 37.0接受:text / html,application / xhtml xml,application / xml; q = 0.9,/; q = 0.8 Accept-Language:en-US,en; q = 0.5 Accept-Encoding:gzip,deflate来源:http://localhost:1968 Access-Control-Request-Method:POST Access-Control-Request-Headers:cache-control,pragma Connection: keep-alive Pragma:no-cache Cache-Control:no-cache
响应 Headers :
主持人:tempuri.org用户代理:Mozilla / 5.0(Windows NT 6.1; WOW64; rv:37.0)Gecko / 20100101 Firefox / 37.0接受:text / html,application / xhtml xml,application / xml; q = 0.9,/; q = 0.8 Accept-Language:en-US,en; q = 0.5 Accept-Encoding:gzip,deflate来源:http://localhost:1968 Access-Control-Request-Method:POST Access-Control-Request-Headers:content-type Connection:keep- alive Pragma:no-cache Cache-Control:no-cache
1 回答
3件事:
[WebService(Namespace =“”)]
在asmx中声明Web方法时,不应将其声明为static
您需要使用[ScriptService]属性配置Web服务以处理来自客户端脚本的调用 .
[System.Web.Script.Services.ScriptService]