我有以下 asmx 服务
[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 DinnerService : System.Web.Services.WebService
{
List<Dinner> dinners;
public DinnerService()
{
dinners = new List<Dinner>();
}
[WebMethod]
[ScriptMethod(UseHttpGet = false, ResponseFormat = ResponseFormat.Json)]
public Dinner HelloWorld()
{
var dinner = new Dinner { DinnerID = 1, Host = "Ahsan", RSVP = "Some People", Venue = "Lahore" };
return dinner;
}
}
我在 Webform 的页面加载事件上从 jQuery 调用它,如下
$(function () {
$.ajax({
type: 'post',
url: 'http://localhost:1901/DinnerService.asmx/HelloWorld',
dataType: 'json',
data:{},
contentType: "application/json; charset=utf-8",
success: function (data) {
$('#res').html(data.d.DinnerID);
alert(data.d.Host);
}
});
});
它在 IE 上工作正常,但在 Firefox 和 Chrome 中它显示内部服务器错误,响应如下:
Request format is unrecognized for URL unexpectedly ending in '/HelloWorld'
这对我来说很奇怪,为什么它可以在一种浏览器上运行,而不能在其他浏览器上运行。我使用 Visual studio 2010 和 .NET 3.5 来提供服务。对于这种特殊情况,使用 WCF 并不是一个选择。网上的回复无法帮助我解决问题。这里究竟发生了什么?
最佳答案
对 web.config 文件进行以下设置将解决该问题。 请参阅follownig发布。
<configuration>
<system.web>
<webServices>
<protocols>
<add name="HttpGet"/>
<add name="HttpPost"/>
</protocols>
</webServices>
</system.web>
</configuration>
关于asp.net - 从 JavaScript 调用 asmx 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9463843/