AJAX POST JSON to .NET Webservice gives 500 Internal Server Error - asp.net

I am trying to consume a .NET webservice with AJAX and want a JSON response. Everything works fine. I have used fiddler and get the appropriate Json returnet. also using the plain URL in the browser gives the appropriate XML.
Even using PHP Curl gives me the right JSON in response but when i am trying to use AJAX i get a "500 Internal Server Error".
Any help appriciated, Thanks.
<script>
$(document).ready(function() {
$.ajax({
type: "POST",
url: "http://localhost:9000/APIs/BuyVoucherService.asmx/HelloWorld",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(data) {
alert(data);
},
error: function(data){
alert(data);
}
});
});
</script>

It seems that you have omitted a data definition in your request, try to add something like this:
data: "{}",

The problem i have realized is that this wont work because of cross domian issues. the solution to get the AJAX call to work with a cross domain solution is to use JSONP. http://www.json-p.org/

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.

Ajax call to ASP.NEt Web Api method. Parser error when it is done from HTTPS page

$.ajax({
url: "api/basket",
type: "GET",
dataType: "json",
error: function (request, status, error) {
alert(request.responseText);
},
success: function (data) {
Process(data);
}
});
I use ASP.NEN Web forms, .Net Framework 4.0, there is an ajax call above which I make . And when it is done from normal HTTP page it gives me data, But if I make this call being on HTTPS page it returns parserror "Unexpected token <"
What is wrong?
Your ajax request isn't returning JSON, it is returning HTML or XML. Thus, when jQuery attempts to parse the response, the first character is sees is < and it throws the parse error.
Use a debugging tool such as fiddler to see exactly what your request returns.

How do I do an AJAX post to a url within a class library but not the same IIS Web Application?

I have been working with ajax and there has been no problems below is how my ajax post code look like:
$.ajax({
type: "POST",
url: '<%=ResolveUrl("TodoService.asmx/CreateNewToDo")%>',
data: jsonData,
contentType: "application/json; charset=utf-8",
datatype: "json",
success: function () {
//if (msg.d) {
$('#ContentPlaceHolder1_useridHiddenField').val("");
$('#ContentPlaceHolder1_titleTextBox').val("");
$('#ContentPlaceHolder1_destTextBox').val("");
$('#ContentPlaceHolder1_duedateTextBox').val("");
alert('Your todo has been saved');
// }
},
error: function (msg) {
alert('There was an error processing your request');
}
});
However, the problem came up when I try to get the url to a webservice that is located in a class library within the same solution.
This ASP.Net
says If you want to put the webservice in the classlibrary, you could try placing the Webservice.asmx.cs file in the class library and place the Webservice.asmx file in the web application project, and then using jquery to consume it in the .aspx page
If it's a different application than yours that's considered an XSS (Cross-site scripting) and it is not allowed.
You could however wrap the call to the external service in your own application (let's say in a REST service) and just call your service from jquery

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
}**
});
}

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