javascript post 500 serrver internal error - asp.net

i'm using ajax and web methods and when i execute this code appear an error thar say
post POST 500 (Internal Server Error)
but this path Exist!!!
this code is i'm using in this moment
function fnSendID() {
$.ajax({
type: "POST",
url: "GMap.aspx/SendCommands",
data: '{IDMobile: "'+$("#<%=Ddl_MobileCustomer.ClientID%>").val()+'"}',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
console.log("Entro : " + response.d);
},
failure: function (response) {
console.log("Fallo : " + response.d);
}
});
<WebMethod()> _
Public Shared Function SendCommands(ByVal IDMobile As String) As String
'iIDMobile = GetMobileID(iIDMobile)
Dim sResponse As String = IDMobile + "Buenas"
Return sResponse
End Function

A 500 means the path you are hitting caused an error on the server side not that the path doesn't exist.

Debug the code and set a breakpoint at Dim sResponse As String = IDMobile + "Buenas".
Does it reach here? Is there any exception before the return?

Related

Passing multiple parameters with Ajax

I have a function in my .aspx page where I collect the first name and last name from the user and would like it to pass back to the server side code(c#). Ajax is being used and runs fine when one parameter is passed, although when a second parameter is passed I receive an error : can someone shed a little light, thanks
ERROR:
Failed to load resource: the server responded with a status of 500 (Internal Server Error)
POST http://ingleParam.aspx/GetCurrentTime 500 (Internal Server Error) jquery-.11.1.min.js:4
send jquery-1.11.1.min.js:4
m.extend.ajax jquery-1.11.1.min.js:4
ShowCurrentTime SingleParam.aspx:12
onclick
.ASPX Code
function ShowCurrentTime() {
$.ajax({
type: "Post",
url: "SingleParam.aspx/GetCurrentTime",
data: { name: $("#<%=txtUserName.ClientID%>").val(),
lastName: $("#<%=txtLastName.ClientID%>").val() },
contentType: "application/json; charset=utf-8",
dataType: "json",
success: OnSuccess,
failure: function (response) {
alert(response.d);
}
});
}
function OnSuccess(response) {
alert(response.d);
}
Server Side Code:
[WebMethod]
public static string GetCurrentTime(string name, string lastname) {
return "Hello " + name + Environment.NewLine + "The current time is " + DateTime.Now.ToString();
}
I've had issues with sending json objects to webmethods. I've had success when I stringify the data being sent across. Like so:
data: JSON.stringify({name: $("#<%=txtUserName.ClientID%>").val(),
lastName: $("#<%=txtLastName.ClientID%>").val()}),
Give that a try and see if it helps.

jquery ajax posting problems getting null value in alert

I'm using xml service to get response but i get null response from service.please help me solve this problem.............
By this service i am sending username and password but both wrong and right is going to succeess part because my service will return nulll value but it will be work fine in POSTMAN.........
<script>
function callXMLConnection() {
alert("call Xml method call");
var un=$("#user").val();
var pw=$("#pwd").val();
var myurl="http://192.162.1.153/EServices/retrieve.aspx";
$.support.cors = true;
$.ajax({
data :"",
type: "POST",
url:myurl,
dataType: "xml",
contentType: "application/xml; charset=utf-8",
crossDomain:true,
success: function(data, textStatus, jqXHR){
alert("text : "+data);
},
error: function(jqXHR, textStatus, errorThrown) {
alert("No data found."+jqXHR);
}
});
}
$(document).unbind('pageinit').bind('pageinit', function () {
$("#userInfo").click(function () {
callXMLConnection();
});
});
</script>
this is my get authentication method
Private Sub GenerateXMLPostMethod(ByVal Format As String, ByRef iostream As Stream)
Try
Dim WSCOMMAND As String, TMPSTR As String As String
WS_USERID = Page.Request.Item("username")
WSCOMMAND = Page.Request.Item("command")
Select Case WSCOMMAND
Case "get_authentication"
Application_Error("Responded at:" & System.DateTime.Now)
WSCOMMAND = Page.Request.Item("username")
TMPSTR = Page.Request.Item("password")
DeviceID = Page.Request.Item("deviceid")
TMPSTR = Page.Request.Item("command")&"&username"&Page.Request.Item("username") & "&password"&Page.Request.Item("username")
TMPSTR = Page.Request.Item("password") 'Passowrd
LBFN_GET_USER(WSCOMMAND, TMPSTR, iostream)
End Select
Catch ex As Exception
Application_Error("GenerateXMLPostMethod :" + ex.Message)
End Try
In code there is no data(username, password ) pass to the server, modify the your code, with
data:"" // sending null value
use like this
data:{usename:un,password:pw}
In server page you can get the values from username,password using post method

unable to get correct status code in AJAX Error handler

I am not able to get correct error code in the error handler of AJAX request. Everytime the error occurs, it returns me statusCode = 500. I tried to set it explicitly in my service as HttpContext.Current.Response.StatusCode = 403;, but still it gives me status = 500.
This is how my AJAX request looks like:
$.ajax({
type: "POST",
url: "Services/someSvc.asmx/SomeMethod",
cache: true,
contentType: "application/json; charset=utf-8",
data:"{}",
dataType: "json"
error: ajaxFailed
});
function ajaxFailed(xmlRequest) {
alert(xmlRequest.status + ' \n\r ' + //This is always 500.
xmlRequest.statusText + '\n\r' +
xmlRequest.responseText);
}
What am i missing here?
Looks like you were almost there, here is an example [WebMethod] that would throw a StatusCode 403.
[WebMethod]
public static string HelloWorld(string name)
{
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.StatusCode = 403;
return null;
}
Here is the calling jQuery Code.
$(document).ready(function ()
{
var jsonRequest = { name: "Zach Hunter" };
$.ajax({
type: 'POST',
url: 'Demo.aspx/HelloWorld',
data: JSON.stringify(jsonRequest),
contentType: 'application/json; charset=utf-8',
dataType: 'json',
success: function (data, text)
{
$('#results').html(data.d);
},
error: function (request, status, error)
{
$('#results').html('Status Code: ' + request.status);
}
});
});
If you don't return a value as specified in your method signature you'll get a status code 500 returned.
According to the documentation:
error(jqXHR, textStatus, errorThrown)
A function to be called if the request fails. The function receives
three arguments: The jqXHR (in jQuery 1.4.x, XMLHttpRequest) object, a
string describing the type of error that occurred and an optional
exception object, if one occurred. Possible values for the second
argument (besides null) are "timeout", "error", "abort", and
"parsererror". When an HTTP error occurs, errorThrown receives the
textual portion of the HTTP status, such as "Not Found" or "Internal
Server Error." As of jQuery 1.5, the error setting can accept an array
of functions. Each function will be called in turn. Note: This handler
is not called for cross-domain script and JSONP requests. This is an
Ajax Event.
So change your code to something more like this:
$.ajax({
type: "POST",
url: "Services/someSvc.asmx/SomeMethod",
cache: true,
contentType: "application/json; charset=utf-8",
data:"{}",
dataType: "json",
error: ajaxFailed (jqXHR, textStatus, errorThrown)
});
function ajaxFailed(jqXHR, textStatus, errorThrown) {
alert(errorThrown + ' \n\r ' + textStatusText);
}
You might also find this answer provides some additional info.

unknown web method using ajax/jquery

ERROR: unknown web method DoIt Parameter name: methodName
I'm trying to pass a date into a DB Query function backended by VB.NET but am having problems with the webside of things.
var dat = $("#Date").val(); //textbox with a date
$.ajax({
type: "POST",
url: "file.aspx/DoIt",
cache: false,
contentType: "application/json; charset=utf-8",
data: {param:dat},
dataType: "json",
success: function (data, status) {
var response = $.parseJSON(data.d);
alert(response.message);
alert(status);
},
error: function (xmlRequest) {
alert(xmlRequest.status + ' \n\r ' + xmlRequest.statusText + '\n\r' + xmlRequest.responseText);
}
});
The file.aspx.vb file:
(at the end of the file)
<System.Web.Services.WebMethod()> _
Public Function DoIt(ByVal param As String) As String
UpdateDB(param) 'function is above
End Function
I'm just not entirely sure whats going wrong or what it means ;/
Check out this answer. You may need to declare the function as Shared
<System.Web.Services.WebMethod()> _
Public Shared Function DoIt(ByVal param As String) As String
UpdateDB(param) 'function is above
End Function
Something that might be worth checking out is to ensure that your database is setup to receive a datetime datatype.
Also something to try in your web-service declaration:
<ScriptMethod(ResponseFormat:=ResponseFormat.Json)> _
<WebMethod()> _
Public Function DoIt(ByVal param As String) As String
UpdateDB(param) 'function is above
End Function
Reference: webservice - unknown web method parameter name methodname

how to handle object passed by jquery on the server

I have this Ajax call to an webMethod
$.ajax({
type: "post",
url:"Default.aspx/MessagesCount",
data: ko.toJSON({ tasks: self.tasks }),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (result) {
var msg = $.parseJSON(result.d);
alert(msg.Tasks[0].title)
},
error: function (resultx) { alert(resultx); }
});
the data send is and object with the following values
{"tasks":[{"title":"PRIVMSG001"},{"title":"PRIVMSG002"},{"title":"PRIVMSG003"},{"title":"PRIVMSG004"},{"title":"PRIVMSG005"},{"title":"PRIVMSG006"}]}
The web method is
<WebMethod()> _
Public Shared Function MessagesCount(ByVal tasks As Object) As String
'How to handle tasks object, using it properties, values exc.
'Testing return dum json
Return "{""Tasks"": [{""title"": ""PRIVMSG001""},{""title"": ""PRIVMSG002""},{""title"": ""PRIVMSG003""}]}"
End Function
How to handle the object send by the client on the server, need to be able to use those values and properties.
Thanks

Resources