ASP.NET + jQuery AJAX - getting 500 server error - c#

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.

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:

Ajax URL - Incorrect .net #c

Been following some tutorials, as i'm learning C#, however was just attempting to make an ajax call to the server, i'm using my localhost.
As far as I can tell i'm doing its right, but it's obviously not.
Maybe it my folder structure or just the name of the file.
The Default.aspx file is at the project root
Here is my ajax call
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "Default.aspx",
dataType: "json",
data: "{'FirstName':'test','LastName':'test1','City':'test3','EmailID':'test4'}",
success: function (data) {
console.log("Woo!");
},
error: function (result) {
console.log("fail!");
}
});
I know eventually it will have to call a method within the file, but its not even finding it at the moment.
Thanks in advance, James
You can use controller Home and create the one action.
Example:
public ActionResult FirstAjax()
{
return Json(true, JsonRequestBehavior.AllowGet);
}
After that your jquery ajax is:
$.ajax({
type: "GET",
contentType: "application/json; charset=utf-8",
url: "Home/FirstAjax",
dataType: "json",
data: "",
success: function (data) {
console.log("Woo!");
},
error: function (result) {
console.log("fail!");
}
});

Calling Web Service from jQuery 1.9.2

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

Calling REST Api with JSONP & JQuery

I have created REST API in MVC4, it is working fine when I compose request from fiddler. But in my application, I need to call through jsonp because it would cross domain request. But when I'm calling this service it gives me error as shown below:
Jquery JsonP Call ..
$.ajax({
type: "POST" ,
url: "http://127.0.0.1:81/api/sites/GetDomainAvailability?apikey=asfasfdsf&callback=?",
data: { SubDomain: subDomain, ParentDomain: parentDomain, ResellerId: resellerId },
cache: false,
contentType: "application/json; charset=utf-8",
success: function (response) {
if (callback)
callback(response.d);
},
error: function (response) {
if (callback)
error(response.d);
},
});
Error:
Right now you are not doing JSONP. It's still POST request. To make it JSONP you need simply to add dataType: "jsonp" to you $.ajax() call. You can also remove some other redundancy parameters like content-type and 'callback' param (but that's optional). So, your code should looke like:
$.ajax({
url: "http://127.0.0.1:81/api/sites/GetDomainAvailability?apikey=asfasfdsf",
data: { SubDomain: subDomain, ParentDomain: parentDomain, ResellerId: resellerId },
datatype: "jsonp",
cache: false,
success: function (response) { /* ... */ },
error: function (response) { /* ... */ },
});
Be also ready, that your request will be transformed to a GET one and will look like
/GetDomainAvailability?apikey=key&callback=jquery123&SubDomain=sss&ParentDomain=ppp&ResellerId=123&_=4398572349857
So, prepare your server-side code for that.

How do I return the current domain of my project in javascript and ASP.NET?

I have some javascript that requires a specific URL to call an ASP.NET web service.
When I run the application locally, the url is something like: http://localhost:123456/ProjectName/Default.aspx
But when I upload the application, the domain will change to:
http://myDomain.com/Default.aspx
What's the best way to capture the current URL path and pass it in as a variable to my javascript? Here is my javascript:
<script type="text/javascript">
$(document).ready(function () {
$("#autoComplete").autocomplete({
source: function (request, response) {
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "http://localhost:123456/ProjectName/Default.asmx/SkillsList", //CHANGE THE URL!!
//url: "http://myDomain.com/Default.asmx", //The other URL...
data: "{'like':'" + request.term + "'}",
datatype: "json",
async: true,
success: function(data) {
response(data.d);
},
error: function(result) {
alert("error");
}
});
},
minLength: 2
});
});
</script>
I'm using ASP.NET if that helps at all.
Thanks!
post to relative path
url: "/ProjectName/Default.asmx/SkillsList"
Looks like you're writing out to the aspx page. Given that, you should be able to just have ASP.Net tell you via something like
url: "<%= Request.ApplicationPath%>/Default.asmx/SkillsList"

Categories