问题
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":"test@test.com"}:
_:1401518889467
Response Headersview source
回答1:
Please check the parameter username is being used in the mentioned contract method. Because, your error is showing like the missing of username parameter.
Ensure this and post your reply soon.
回答2:
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.
来源:https://stackoverflow.com/questions/23967360/jsonp-with-asmx-error-500