我有一个.asmx文件,我正在从web表单页面上的jquery ajax调用中调用它。返回的响应是404错误。我不确定我做错了什么,因为我已经尝试了所有我发现的例子,但都没有效果。
AJAX:
function clearTemp(_websiteID) {
$.ajax({
type: "POST",
url: "ScheduleImportWebService.asmx/HelloWorld",
contentType: "application/json; charset=utf-8",
data: JSON.stringify({ websiteID: _websiteID }),
dataType: "json",
success: function (response) {
alert(response.d);
},
failure: function (response) {
alert("Failure: Could not clear temp table: " + response.d);
},
error: function (response) {
alert("Error: Could not clear temp table: " + response.d);
}
});
}
ASMX代码段:(我尝试过[System.Web.Script.Services.ScriptService]也被注释掉了,但仍然得到了相同的响应(
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
[System.Web.Script.Services.ScriptService]
public class ScheduleImportWebService : System.Web.Services.WebService
{
[WebMethod]
public string HelloWorld(string websiteID)
{
return "Hello World";
}
}
错误是因为您忘记了通过ajax调用在URL中设置主机。
试试这样的东西:
http://localhost/ScheduleImportWebService.asmx/HelloWorld