Simple AJAX not working - asp.net

I have this AJAX code, but it doesn't seem to throw the 'alert' method. Instead, nothing happens. I looked at it with Fiddler and got this error message:
{"Message":"There was an error processing the request.","StackTrace":"","ExceptionType":""}
I'm trying to call a web method in the code-behind called MyWebMethod:
$.ajax({ type: "POST",
url: "Test.aspx/MyWebMethod",
data: "{" + username + "}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function() {
alert("success");
},
fail: function() {
alert("Fail");
}
});
The web method worked fine when I had a script manager on the page, but I want to remove the script manager and thought that using AJAX would be the best way.
Thanks

You have custom errors enabled in the web.config. Therefore, the exception returned will be generic (mostly blank) and the same every time. This makes it difficult to debug.
To see the real exception, temporarily disable custom errors. Here is how to do that for web services only, if you need that granularity.

I think if you change fail to error, you'll get the second alert box.
[Edit] I think if you then change
data: "{" + username + "}"
to
data: "{ 'username': '" + username + "' }"
you'll get the first alert, although it's hard to know that without seeing the service you're calling.

Related

ASP.Net, jQuery, Ajax and a parseError

I have tried to see if there is anything on this great site for this (and there probably is), so I am sorry if I have missed it.
Anyway, I am calling a WCF endpoint using jQuery Ajax under ASP.Net.
The Web service call is working fine in Fiddler and returns the following data:
{"d": {"__type":"ProcessedTotals:#","DailyError":"0","DailyProcessed":"0","TotalError":"48","TotalProcessed":"70"}}
This is my code
function GetData() {
alert('Ajax Start');
$.ajax({
dataType: "jsonp",
data: "{}",
contentType: "json",
url: "http://localhost/abc123/WebManagement.svc/ReturnTotals",
success: (function (data) {
alert(data[0].TotalProcessed);
//$("#TotRecs").html(data.d.TotalProcessed);
//$("#TotErr").html(data.d.TotalError);
//$("#DayTot").html(data.d.DailyProcessed);
//$("#DayErr").html(data.d.DailyError);
}),
error: function (xhr, status, error) {
alert("FAILED:" + status);
}
})
alert('Ajax End');
}
Whilst debugging in Firefox the line that causes the error is SUCCESS:
I know that it is using JSONP, as I am having to access the data from a different website.
Thanks
Paul
$.ajax({
dataType: "jsonp",
url: "http://localhost/abc123/WebManagement.svc/ReturnTotals",
success: (function (data) {
console.log(data);
})
});
You were doing some weird stuff there. Trying to send an empty object literal along with the call, which doesn't make any sense. Also, your error callback won't work with a jsonp request. Try the above, it should work fine. If not, please mention the error that you're getting.

In ASP.NET with JQuery, how would you define and handle exceptional conditions

I use $.ajax to call a .asmx to get JSON data back. The server side process will meet some exceptional conditions which will give back to client error response for displaying error message.
How would you design the error hanlding mechanism? I mean what would you make server side retrun and let client handle the error?
Thanks.
If it's just an error message (a string), then you can just return that string and set the status to 500 so jQuery recognizes it's not a successful request. Then just use your error handler (or a global .ajaxError() handler for all requests) to do what you want with that message.
For example a simple way to handle all errors like this (instead of per-$.ajax() call) would be:
$(document).ajaxError(function(e, xhr) {
alert("There was an error, server responded with: " + xhr.responseText);
});
ther's an error handler in jquery just like the success function, you need to handle it:
function CallWebService(ParentPage) {
$.ajax({
type: "POST",
url: "../Admission/AdmissionService.asmx/LoadLocations",
data: "{'ParentPage':'" + ParentPage + "'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
$('.innerHospitalCensusdiv').html(data.d);
BindSelectLocation();
},**error: function(ex){
//handle error here
}**
});
}

why does the 'error' function of $.ajax execute even when the request to invoke a webmethod succeeds?

I am using jquery ajax method to invoke a webmethod upon clicking a 'span'.This is webmethod is in one of my aspx pages and I am invoking it from the master page using the following code.
$(document).ready(function(){
$("#btn").click(function() {
$.ajax({
type: "POST",
url: "Default.aspx/removedata",
data:"{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success:function(msg) {
$("li#search").removeClass('current');
$("li#search").addClass('hide');
$("#tabnew").addClass('hide');
window.location="Result.aspx";
},
error:function(xhr, status, error) {
alert("error");
//var err = eval("(" + xhr.responseText + ")");
// Display the specific error raised by the server
//alert(err.Message);
console.log(xhr.statusText);
}
});
});
});
when I click the span I can see the webmethod getting invoked(by debugging ),but even before the webmethod starts executing I get the alert 'error' and I see (an empty string) message being logged into the firebug console.
As far as I know the 'error' function gets executed only if the ajax request fails.But I can see the webmethod getting executed.I do not understand why the error function is executing even then.
Could someone please help me with this.
Thanks
The error handler is executed if the server side script returns an error code different than 200. You could use FireBug to inspect what exactly is happening under the covers.

JQuery AJAX Web Service call on SSL WebServer

I am making a POST to a webservice that is local to the webserver. Everything works great until I host the site at my SSL enabled webserver. The webservice path is relative, meaning, I am making no reference to the protocol. eg. /webservices/method.asmx
The POST results in a runtime error. Has anyone seen this before?
$.ajax({
type: "POST",
url: theURL + "/" + method,
data: body,
success: function (msg) {
alert("Data Saved: " + msg);
},
error: function (msg) {
alert("Broken: " + theURL + "/" + method + msg.responseText);
}
});
What happens if you specify an absolute url, including the protocol https:// piece... I usually put a javascript variable in my masterpages/template for "baseURL" for the prot://host:port/apppath/ reference... where port is only included if not the default. Haven't, iirc, seen such an issue.
Because this project is done in .NET 2.0.. the web method seems to handle the input data differantly. I was able to get around this by just construction a query sting and passing it rather than a JSON object.

Does authentication/authorization stop Jquery from calling a page method?

I have the following JQuery code that worked perfect in C#/Asp.net 2.0 to call a page method in the default.aspx page. Now I am trying to call a shared page method in VB.Net and the page method is not firing, I believe because of security or it is not finding it.
The page that this shared vb method is in doesn't allow anonymous access, so I was thinking that is the problem or it is a path problem to finding the method. I am just guessing here. In my C# test app the static webmethod was in the default.aspx page with no security. Thanks for any advice or help!
$.ajax({
type: "POST",
url: "Orders.aspx/GetMailPieceGroupsByAdFundTypeId",
data: myDataToSend,
contentType: "application/json; charset=utf-8",
dataType: "json",
//error: function(XMLHttpRequest, textStatus, errorThrown) {alert(errorThrown); this;},
success: function(data, textStatus){alert(success);mailPieceGroups = eval('(' + data + ')'); startSlideShow(mailPieceGroups); }
});
My issue was the path provided was not correct which caused the .ajax call not to be able to locate the method to call it.
This is correct for my scenario:
url: "../Orders.aspx/GetMailPieceGroupsByAdFundTypeId",

Resources