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!");
}
});
Related
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:
When I try to send json JSON.stringify(coords); or above code I get error message but I try when the data is empty like data:{} the code works correctly. How can I solve this problem?
$.ajax({
type: "POST",
data: {"yagiz":"aabb"},
dataType: 'json',
url: url,
crossDomain:true,
async: false,
contentType: "application/json; charset=utf-8",
success: function (response) {
$("#Content").text(response.d);
console.log(response.d);
},
failure: function (response) {
alert(response.d);
console.log(response.d);
}
});
Web Method
[System.Web.Services.WebMethod]
public static string GetLocationPolygon(string location)
{
return location;
}
You can debug on server side as 500 internal error raised at server side only. You can catch and log the exact exception.
try this code
$.ajax({
type: "POST",
data: JSON.stringify({"location":"aabb"}), //Change here
dataType: 'json',
url: url,
crossDomain:true,
async: false,
contentType: "application/json; charset=utf-8",
success: function (response) {
$("#Content").text(response.d);
console.log(response.d);
},
failure: function (response) {
alert(response.d);
console.log(response.d);
}
});
I am using lot of .NET WebMethod calls in my web application using jQuery. My calls are not working in the expected order. The first time the page loads it works fine, and after reloading the page, the WebMethod calls are disordered. How do I make it always work one by one in the expected order?
I am using jQuery $.ajax():
$.ajax({
type: "POST",
url: "",
data: '',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) { },
error: function (msg) { alert(msg.error); }
});
Add async: false option in ajax. By default Ajax make async call hence order is not guaranteed.
$.ajax({
type: "POST",
async : false, // Add this
url: "",
data: '',
contentType: "application/json;
charset=utf-8",
dataType: "json",
success: function (response) { },
error: function (msg) { alert(msg.error); }
});
Please help, I'm stuck here. I have a problem with passing input parameter to my C# controller. I tried really a lot of things I found here, but still there's some mistake.
Here is code:
var a = $.ajax({
type:"GET",
url: "/Weather1/Weather_get",
data: "location=Paris%",
contentType: 'application/json; charset=utf-8',
cache: false,
success: function (data) {
console.log(data);
}, //succes
error: function (data) {
console.log(data);
}//error
}) //ajax;
And here is controller:
[HttpGet]
public JsonResult Weather_get(String location) etc etc...
Everything I tried gives me input location NULL. I know my controller is working fine because I get data from it, but I really need that data to be for specific location, so that's why I need it like this. Also don't want to change it to POST because this is homework problem so I have to have both HttpPost and HttpGet in my controller.
Try this
var place="Paris%";
var a = $.ajax({
type:"GET",
url: '#Url.Action("Weather_get","Weather1")',
data: '{ "location":"' + place+ '"}',
dataType: 'json',
cache: false,
success: function (data) {
console.log(data);
}, //succes
error: function (data) {
console.log(data);
}//error
}) //ajax;
just include the parameter name in your controller and the value you want to pass like this.
$.ajax({
type:"GET",
url: '#Url.Action("Weather_get","Weather1")',
data: { location:paris},
dataType: 'json',
cache: false,
success: function (data) {
console.log(data);
}, //succes
error: function (data) {
console.log(data);
}//error
}) //ajax;
The way ASP.Net binds the HTTP request URL to the model used by your controller is controlled in part by the routing engine.
If you navigate to
/Weather1/Weather_get/Paris
do you see the data from Paris?
If so, you could modify your AJAX this way
$.ajax({
type:"GET",
url: "/Weather1/Weather_get/Paris"
(etc.)
})
You should probably use JSON.stringify.
something like this:
var data = {
location: "Paris%"
};
var a = $.ajax({
type:"GET",
url: "/Weather1/Weather_get",
data: JSON.stringify(data),
dataType: 'json',
contentType: 'application/json',
cache: false,
success: function (data) {
console.log(data);
}, //succes
error: function (data) {
console.log(data);
}//error
}) //ajax;
My ajax code is not passing values to my webservice method .. i think i am not doning it properly. please guide me.
this is my .aspx code:
$(function () {
$.ajax({
type: "POST",
url: "WebService.asmx/InsertRediretTime",
data: "{ 'ReachTime': '21-Nov-11', 'Destination': 'location' }",
contentType: 'application/json; charset=utf-8',
dataType: 'json',
success: function (data, status) {
alert(data.d);
}
});
});
and this is my webservice method
public static void InsertRediretTime(string ReachTime, string Destination)
{
//operational code
}
Thanks in advance
Take out the static keyword from your method.
public void InsertRediretTime(string ReachTime, string Destination)
{
//operational code
}
Try this:
$(function () {
$.ajax({
type: "POST",
url: "WebService.asmx/InsertRediretTime",
data: "ReachTime=21-Nov-11&Destination=location",
contentType: 'application/json; charset=utf-8',
dataType: 'json',
success: function (data, status) {
alert(data.d);
}
});
});
Although your service is expecting id and order, but you're passing ReachTime and Destination - is this correct?
What is the error that you are getting? See tool such as Fiddler (or Firebug on Firefox) to inspect the request/response - see the response for your ajax request - that will help you to troubleshoot the issue.
OTH, you need a ScriptService attribute applied to your web service class. If you are using .NET 2.0/3.5 then you also need configuration entries to register ScriptHandlerFactory handler that is responsible for JSON support in asmx services. See this article for more info regarding configuration: http://encosia.com/asmx-scriptservice-mistakes-installation-and-configuration/
Thanks everyone for your help ... a combination of your help worked for me .. here is the solution:
$(function () {
$.ajax({
type: "POST",
url: "WebService.asmx/InsertRediretTime",
data: '{ ReachTime: "21-Nov-11", Destination: "location" }',
contentType: 'application/json; charset=utf-8',
dataType: JSON,
success: function (data, status) {
alert(data.d);
}
});
});
and,
public void InsertRediretTime(string ReachTime, string Destination)
{
blah blah
}
Thanks again :)
Try this,
in aspx page
$(function () {
$.ajax({
type: "POST",
url: "WebService.asmx/InsertRediretTime",
data: '{ReachTime:21-Nov-11,Destination:location}',
contentType: 'application/json; charset=utf-8',
dataType: 'json',
success: function (data, status) {
alert(data.d);
}
});
});
In webservice
public string InsertRediretTime(string ReachTime, string Destination)
{
//operational code
return stringData;
}