多么痛苦!我正在尝试从 JavaScript 调用 Web 服务,但我似乎找不到让它工作所需的魔法酱。
我的代码看起来像这样:
JavaScript:
$.ajax({
url: "/Services/CompanyContactServices.asmx/AddContact",
type: 'POST',
contentType: "application/json",
dataType: "json",
data: {
companyId: 3725, firstName: firstName, lastName: lastName, email: email
},
success: function (data) {
alert('Success!');
},
error: function (jqXHR, textStatus, errorThrown) {
alert(errorThrown);
}
});
网络服务(asmx.cs):
[WebService(Namespace = CompanyListServices.XmlnsNamespace)]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ScriptService]
public class CompanyContactServices : System.Web.Services.WebService
{
public const string XmlnsNamespace = "mynamespace";
[WebMethod(EnableSession = true)]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string AddContact(int companyId, string firstName, string lastName, string email)
{
return new JavaScriptSerializer().Serialize(0);
}
}
AddContact
我的方法开始时的断点永远不会到达。
error
我的调用的处理程序ajax()
包含以下错误消息jqXHR
:
{ Message":"无效的 JSON 原语:companyId。","StackTrace":"在 System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializePrimitiveObject() 在 System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeInternal(Int32 深度) 在系统.Web.Script.Serialization.JavaScriptObjectDeserializer.BasicDeserialize(String input, Int32 depthLimit, JavaScriptSerializer serializer) at System.Web.Script.Serialization.JavaScriptSerializer.Deserialize(JavaScriptSerializer serializer, String input, Type type, Int32 depthLimit) 在 System.Web。 System.Web.Script.Services.RestHandler.GetRawParamsFromPostRequest(HttpContext context, JavaScriptSerializer serializer) 处 System.Web.Script.Services.RestHandler 处的 Script.Serialization.JavaScriptSerializer.Deserialize[T](String input)。GetRawParams(WebServiceMethodData methodData, HttpContext context) at System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context, WebServiceMethodData methodData)","ExceptionType":"System.ArgumentException"}"
Chrome 报告以下网络数据:
请求网址:.../Services/CompanyContactServices.asmx/AddContact
请求方法:POST
状态码:500 内部服务器错误
谁能看到我错过了什么?