After testing this code, it's working perfectly fine for me.
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!");
}
});
}
Within the same asmx file, there is another method as shown 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 performs an insert into a database and returns the same output as GetPersonTest. However, when I try to call it using JavaScript and JSONP, it results in a 500 server error.
Upon checking in the debugger, the following error message is displayed
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"});
If anyone can provide assistance on the above, that would be greatly appreciated. Thank you.
The header of the method is as follows
<WebMethod(True)> _
<ScriptMethod(UseHttpGet:=True, ResponseFormat:=WebMessageFormat.Json)> _
Public Function GetPerson(ByVal username As String, ByVal emailID As String) As String
End Function
Also, when inspecting the header in the developer tool, it shows the following information
callback:jQuery17106155389654450119_1401518867750
{"username":"testuser","emailID":"<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="671302141327130214134904080a">[email protected]</a>"}:
_:1401518889467
Response Headersview source