Calling Web Service from jQuery 1.9.2 - c#

I am just trying to call the webservice from jQuery. My code is,
function SearchCandidates() {
$("#txtSearchGlobal").autocomplete({
source: function (request, response) {
$.ajax({
type: "POST",
url: "WebService.asmx/HelloWorld",
contentType: "application/json; charset=utf-8",
data: "{}",
dataType: "json",
success: function (data) {
},
error: function (req, status, error) {
alert("ERROR:" + error.toString() + " " + status + " " + req);
}
});
},
minLength: 2,
select: function (event, ui) {
}
});
}
here, am getting an error saying "ERROR : Internal Server Error error [object object]", am not able to find the exact error. I have worked with the same code mentioned above, so many times and I haven't found any issues in that. Am using jQuery 1.9.2 here, and I dont think the version is the problem. Can anyone help me here, thanks in advance.

From your code remove the else from success and check and also place data type as Jsonp The below code may help you
$.ajax({
type: "POST",
url: url,
data: data,
dataType: 'jsonp'
success: function(data){
alert(data)
},
error:function(req, status, error){
alert(JSON.stringify(error))
}
});

Related

Ajax function failing to hit method on code behind on server

I have the following ajax function
var jsonId = JSON.stringify(sortedIDs);
$.ajax({
type: "POST",
data: { ids: jsonId },
datatype: "json",
contentType: "application/json; charset=utf-8",
url: "/Intranet/Dev/TestSortTable.aspx/GetData",
success: function (msg) {
alert(msg.d + "success");
},
error: function (response) {
alert("an error has occured");
}
});
And the following method in the code behind page
[WebMethod]
public static string GetData(string[] data)
{
return "this is the string from the code behind file";
}
The error I am getting is a 500 internal server error. If I add .cs to the TestSortTable.aspx I get a 404 not found error. This is the first time I have implemented an Ajax function and I am at a loss as to what I have done wrong. I should add that sortedIDs is defined elsewhere.
You're not sending the parameters as JSON. You're converting sortedIDs to JSON, but being wrapped into an object that gets sent as URL-encoded data. You need to do:
var json = JSON.stringify({data: sortedIDs);
$.ajax({
type: "POST",
data: json,
dataType: "json",
contentType: "application/json; charset=utf-8",
url: "/Intranet/Dev/TestSortTable.aspx/GetData",
success: function (msg) {
alert(msg.d + "success");
},
error: function (response) {
alert("an error has occured");
}
});
Also, datatype: should be dataType:

ASP.NET + jQuery AJAX - getting 500 server error

I want to call an ASP.NET function from jQuery by AJAX with response.
I have file Controll.aspx where is included javascript code. Next I have /Services/ControllService.asmx, where is the function, which I want call from js.
js code:
$(document).ready(function () {
$('#btn_start').on('click', function () {
$.ajax({
type: "POST",
url: "Services/ControllService.asmx/Start",
data: {},
dataType: "json",
async: true,
contentType: "application/json; charset=utf-8",
success: function (response) {
console.log(response);
},
error: function (err) {
alert("Error:" + err.toString());
}
});
});
});
But I still getting the error 500.
POST http://localhost:56000/Services/ControllService.asmx/Start 500 (Internal Server Error)
k.cors.a.crossDomain.send
n.extend.ajax
Do you have any hints, what do I need to set e.g. in Web.config?
Many thanks.
SOLVED:
1 - I have defined Start function in ControllService.asmx.cs as static.
2 - I have badly configured data. It has to be named by the same way e.g. "sth".
In javascript it should be:
...
url: "Services/ControllService.asmx/Start",
data: JSON.stringify({ sth: "hahaha" }),
dataType: "json",
...
and in ControllService.asmx.cs -> method Start
public string Start(string sth){}
Many, many thanks for your hints.

Cannot get response in ajax success

My AJAX code is below. In this i cannot get the response from server side C# code. But there is no issues with my server side code ( I have checked it by debugging ). From server side i am returning string to this ajax.
$.ajax({
type: "POST",
url: '#System.Web.Configuration.WebConfigurationManager.AppSettings["BaseURL"]' +"provider/GetState",
contentType: 'application/json',
data: {CountryId: Country_Id },
success: function (data) {
alert(data);
}
});
Server side code is below
public string GetState(string CountryId)
{
int i= Convert.ToInt32(CountryId);
var Details = objUserAccount.SP_Getstate(i).ToList();
if(Details.Count>0)
{
return "Success";
}
else
{
return "False";
}
}
Add datatype in your ajax request like this, if datatype is not matched with received data from server then the ajax error will call instead of success
$.ajax({
type: "POST",
url: '#System.Web.Configuration.WebConfigurationManager.AppSettings["BaseURL"]'+ NSSCTProvider/GetState",
contentType: 'application/json',
dataType: "json",
data: {CountryId: Country_Id },
success: function (data) {
alert(data);
},
error: function(data){
alert("Error occured");
}
});
$.ajax({
type: "POST",
url: '#System.Web.Configuration.WebConfigurationManager.AppSettings["BaseURL"]'+ NSSCTProvider/GetState",
contentType: 'application/json',
data: {CountryId: Country_Id },
success: function (data) {
alert(data);
},
error: function(data){
alert("Error occured");
}
});
Either one of the success or failure call back will be initiated if the request is made from this snippet. Might be a server issue, most probably. And make sure that this is the code snippet getting executed. Happens to me all the time.

no data is received from an ajax call

$.ajax({
type: "GET",
contentType: "text/html; charset=utf-8",
url: "secret.aspx",
data: {
plu: $("#Text1").val(),
gh: $("#TextBox1").val(),
sid: $("#TextBox2").val()
},
dataType: "html",
success: function(data) {
$("#result").html(data);
}
});
I am making a call to the aspx page, the call goes correctly. Data is entered in database, but values are not returned to the page
The return statements are as follows:
Response.Write("hello");
Response.End();
It should work. Perhaps an error's occurring with secret.aspx. To find out, add an error setting to the ajax call so you get to know about any errors. You could also add an alert to display the returned data, in case it's something to do with your result element:
success: function (data) {
alert(data);
$("#result").html(data);
},
error: function (xhr, ajaxOptions, thrownError) {
alert(xhr.status + ' - ' + xhr.responseText);
alert(thrownError);
}
});

WCF REST service returning JSONP no error but no data either

I can see the data in the Sources tab in Chrome which shows json?callback=jQuery1324235... if I click that item I can see my JSON string.
I'm not getting the cross domain error or any error in Chrome. My ajax is the following, the error section just returns Success as the status. The debugger never stops in the success block though.
$.ajax({
cache:false,
type: 'GET',
async: false,
timeout: 60000,
data: "{}",
contentType: "application/json",
url: 'http://urlsvc/json',
dataType: 'jsonp',
success: function(data) {
var test = data;
$.each(data,function(i,j){
list += "<h2 class='name'>" + j.TraitName + "</h2><p class='category'>" + j.Category + "</p><p class='desc'>" + j.Description + "</p>";
});
},
error: function (xhr) {
err += xhr.statusText;
},
complete: function(){
$('#list1').html(list);
}
});
I got it! It was my service, I missed the bindingConfiguration attribute in the web.config. Thanks everyone!

Categories