问题描述
当我尝试这个它的工作原理perfact
函数test(){
调试器;
$阿贾克斯(网址:{url:http://testweb.com/myAPI.asmx/GetPersonTest
键入:GET,
的contentType:应用/ JSON的;字符集= UTF-8,
数据:{用户ID:1},
数据类型:JSONP
成功:函数(JSON){
警报(json.UserID +''+ json.FirstName +''+ json.LastName);
},
错误:函数(){
警报(打错误FN!);
}
});
}
在同一个ASMX,我有以下
称为另一种方法 函数Post_test(){VAR的newURL = window.location.protocol +'//'+ window.location.host +/+ window.location.pathname;
VAR用户=的document.getElementById(用户名)的价值。
。VAR电子邮件=的document.getElementById(电子邮件)的价值;
调试器;
$阿贾克斯(网址:{url:http://testweb.com/myAPI.asmx/GetPerson
键入:GET,
的contentType:应用/ JSON的;字符集= UTF-8,
数据:JSON.stringify({用户名:用户,EMAILID:电子邮件})
数据类型:JSONP
成功:函数(JSON){
警报(json.UserID +''+ json.FirstName +''+ json.LastName);
},
错误:函数(JSON){
警报(打错误FN!);
}
});
}
这方法做一个INSERT INTO数据库,并返回相同的GetPersonTest。但是,当我把这种通过JavaScript和JSONP它给了500服务器错误
当我在调试器中检查,它给下面的错误
jQuery17106155389654450119_1401518867750({消息:无效的Web服务调用,参数缺失值:\\ u0027username \\ u0027。,堆栈跟踪:在System.Web.Script.Services .WebServiceMethodData.CallMethod(对象的目标,IDictionary`2参数)\\ r \\ n在System.Web.Script.Services.RestHandler.InvokeMethod(HttpContext的背景下,WebServiceMethodData methodData,IDictionary`2 rawParams)\\ r \\ n在System.Web程序。 Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext的背景下,WebServiceMethodData methodData),ExceptionType:System.InvalidOperationException});
任何人都可以请帮我在下面。谢谢
方法的头
<的WebMethod(真)> _
< ScriptMethod(UseHttpGet:= TRUE,ResponseFormat:= WebMessageFormat.Json)GT; _
公共职能GetPerson(BYVAL用户名作为字符串,BYVAL EMAILID作为字符串)作为字符串结束功能
当我检查的开发工具标题是低于
回调:jQuery17106155389654450119_1401518867750
{用户名:testuser的,EMAILID:[email protected]}:
_:1401518889467
响应Headersview源
您可以尝试用以下变化:
-
从数据参数删除JSON.stringify',所以它应该看起来像
数据:{用户名:用户,EMAILID:电子邮件}
-
响应格式被提及作为WebMessageFormat.Json,所以更改数据类型为JSON,代替JSONP。
-
由于正在发送的查询字符串,而不是JSON,你可以放心地忽略的contentType(或者可以删除只是应用/ JSON')
希望这有助于。
when i try this it works perfact
function test() {
debugger;
$.ajax({ url: "http://testweb.com/myAPI.asmx/GetPersonTest",
type: "GET",
contentType: "application/json; charset=utf-8",
data: { userid: 1 },
dataType: "jsonp",
success: function(json) {
alert(json.UserID + ' ' + json.FirstName + ' ' + json.LastName);
},
error: function() {
alert("Hit error fn!");
}
});
}
in the same asmx, i have another method called as below
function Post_test(){
var newURL = window.location.protocol + '//' + window.location.host + '/' + window.location.pathname;
var user = document.getElementById('Username').value;
var email = document.getElementById('email').value;
debugger;
$.ajax({ url: "http://testweb.com/myAPI.asmx/GetPerson",
type: "GET",
contentType: "application/json; charset=utf-8",
data: JSON.stringify({ username: user, emailID: email}),
dataType: "jsonp",
success: function(json) {
alert(json.UserID + ' ' + json.FirstName + ' ' + json.LastName);
},
error: function(json) {
alert("Hit error fn!");
}
});
}
This method do a insert into database and return the same as GetPersonTest. But when i call this through javascript and jsonp it gives 500 server error
When i check in debugger , it gives the below error
jQuery17106155389654450119_1401518867750({"Message":"Invalid web service call, missing value for parameter: \u0027username\u0027.","StackTrace":" at System.Web.Script.Services.WebServiceMethodData.CallMethod(Object target, IDictionary`2 parameters)\r\n at System.Web.Script.Services.RestHandler.InvokeMethod(HttpContext context, WebServiceMethodData methodData, IDictionary`2 rawParams)\r\n at System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context, WebServiceMethodData methodData)","ExceptionType":"System.InvalidOperationException"});
Can anyone please help me on below. thanks
the head of method is
<WebMethod(True)> _
<ScriptMethod(UseHttpGet:=True, ResponseFormat:=WebMessageFormat.Json)> _
Public Function GetPerson(ByVal username As String, ByVal emailID As String) As String
End Function
And when i check the header in developer tool it is below
callback:jQuery17106155389654450119_1401518867750
{"username":"testuser","emailID":"[email protected]"}:
_:1401518889467
Response Headersview source
Can you try with the following changes :
Remove 'JSON.stringify' from data parameter, so it should look like
data : {"username": user, "emailID": email}
The response format is mentioned as "WebMessageFormat.Json", so change the dataType to "json", in place of "jsonp".
As the is being sent as querystring and not JSON, you can safely omit "contentType" (or can remove just 'application/json').
Hope this helps.
这篇关于与ASMX错误500 JSONP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!