i have an ajax request in a page say Test.aspx
$.ajax({
type: "POST",
url: "Test.aspx/TestMethod",
data: "{ 'Parameter':'Parameter'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (res) {
alert('success');
},
error: function (xhr, ajaxOptions, thrownError) {
alert('error getting Regions');
}
});
and in the code behind
[WebMethod()]
public static string TestMethod(string Parameter)
{
return "teststring";
}
Issue is control is not going in to success inside ajax, any idea?''
Testtry this
$.ajax({
type: "POST",
url: "Test.aspx/TestMethod",
data: "{ 'Parameter':'Parameter'}",
contentType: "application/json; charset=utf-8",
success: function (res) {
var s = JSON.stringify(res);
alert(s);
},
error: function () {
alert('error getting Regions');
}
});
There are few things you need to make sure you are doing.
First thing you need to add script manager with enable page method property true like below.
<asp:ScriptManager ID="scriptManager" runat="server" EnablePageMethods="true">
</asp:ScriptManager>
See my blog post about How to call ASP.Net page method with jQuery
Check the Page URL and everything. It should be correct and should be exactly same as page name and method name as it is case sensitive.
Check parameter also. There is another good post with parameter is also below for your reference.
http://www.aspsnippets.com/Articles/Call-ASPNet-Page-Method-using-jQuery-AJAX-Example.aspx
Related
I'm calling a WebMethod inside my application. Recently i changed the root config to upgrade the multilingual part.
public static void RegisterRoutes(RouteCollection routes)
{
routes.MapPageRoute("languageHome","{language}/{page}","~/{page}.aspx");
}
Since then it seem that i cannot call the WebMethod with jQuery
function ShowNumeroLame() {
$.ajax({
type: "POST",
url: "Home/GetNumeroLame",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: OnSuccessNumeroLame,
error: function () {
},
failure: function (response) {
}
});
}
I think that the problem is in the URL but i can't find it
Thanks, and sorry for my bad explanation
I solved the problem adding this:
url: '<%=Page.ResolveClientUrl("Home.aspx/GetSpessoriFette") %>',
I have a web form IndexPage.aspx. The script part of this front end code is calling a code-behind function using ajax call.Here's the code:
$.ajax({
type: "POST",
url: '<%=ResolveUrl("~//IndexPage.aspx//SaveXml")%>',
data: JSON.stringify(logic),
contentType: "application/json; charset=utf-8",
dataType: "json",
//async: true,
//cache: false,
success: function (msg) {
alert("AJAX received : " + msg.d);
},
error: function (msg) {
alert("Failed : " + msg.d);
}
});
The "Failed" alert box is shown with return value 'undefined' after I run the code.
The code behind function is:
public static string SaveXml(string logic)
{
return logic;
}
This code-behind function is not getting called. The breakpoint which I have set at the code-behind function is not getting hit. I have tried almost all solutions from stack overflow. Please see if anythig is wrong in this code.
Thanks friends,
The issue was resolved. Checking browser console and network helped me a lot to solve the error.
I did two changes
1) Added [System.Web.Services.WebMethod] to code-behind function
2) Changed my => data: JSON.stringify(logic) ..... to ....=> data: '{ logic:\''+ logic +'\'}'
I did the second change after referring to this link: "Invalid JSON primitive" in Ajax processing
While sending just a string you need to add escape sequence.
Resolved
Ajax call:
$.ajax({
type: "POST",
url: '<%=Page.ResolveUrl("~/WebForm1.aspx/SaveXml")%>',
data: JSON.stringify({ "logic": "logic value" }),
contentType: "application/json; charset=utf-8",
dataType: "json",
//async: true,
//cache: false,
success: function (msg) {
alert("AJAX received : " + msg.d);
},
error: function (msg) {
alert("Failed : " + msg.d);
}
});
Web Method
[WebMethod]
public static void SaveXml(string logic) {
}
Web.config
<system.web>
<webServices>
<protocols>
<add name="HttpGet"/>
<add name="HttpPost"/>
</protocols>
</webServices>
When I am calling server method from AJAX call at that time showing 500 internal server error. And this error also happens sometimes only, while sometimes it is working fine.
I am really confused that what is going on that it is sometimes working and sometimes not working. In fact I didn't change anything after working the code but when I check second day it is coming this type of error.
Here is my code
<input type="button" id="btn_save" value="Save" class="button" />
$(document).on("click", "#btn_save", function (e) {
$.ajax({
type: "POST",
url: "schoolregistration.aspx/EntrySave",
data: JSON.stringify({ schoolName: $('#txt_schoolname').val() }),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: OnSuccess,
failure: function () {
document.getElementById("txt_schoolname").value = "";
alert("Error! Try again...");
}
});
});
function OnSuccess(response) {
document.getElementById("txt_schoolname").value = "";
alert(response.d);
}
[WebMethod]
public static string EntrySave(string schoolName)
{
//Here is the code
}
Sometimes working fine but sometimes not coming call in this entrysave method.
try this:
$.ajax({
type: "POST",
url: "schoolregistration.aspx/EntrySave",
data: { "schoolName": $('#txt_schoolname').val() },
contentType: "application/json; charset=utf-8",
dataType: "json",
success:function (data) {alert('ok');},
error: function () {alert('error');}
});
I want to call the server method through the AJAX call. But when I am click the button and call the AJAX function at that time it shows an error.
Here is my code
<input type="button" id="btn_findsubmit" value="Edit" class="button" />
$(document).on("click", "#btn_findsubmit", function (e) {
var c = $find("<%=cmbobx_search.ClientID %>");
$.ajax({
type: "POST",
url: "schoolregistration.aspx/GetSchoolName",
data: json.stringify({ schoolname: c.get_textboxcontrol().value }),
contenttype: "application/json; charset=utf-8",
datatype: "json",
success: OnSuccessGetSchoolName,
failure: function () {
alert("error! try again...");
}
});
});
[WebMethod]
[ScriptMethod]
public static string GetSchoolName(string schoolName){
//Here is the code
}
Now when I click the button at that time JavaScript button click event is working but the ajax method do not calling the server method GetSchoolName (I know by doing debug mode).
And throws a error that:
ReferenceError: json is not defined
It should be JSON.stringify, not json.stringify
<input type="button" id="btn_findsubmit" value="Edit" class="button" />
<script>
$(document).on("click", "#btn_findsubmit", function (e) {
$.ajax({
type: "POST",
url: "Default.aspx/GetSchoolName",
data: JSON.stringify({ schoolName: "school name" }),
contentType: "application/json; charset=utf-8",
datatype: "json",
success: function (data) {
alert(data.d);
},
failure: function () {
alert("error! try again...");
}
});
});
</script>
[WebMethod]
public static string GetSchoolName(string schoolName)
{
//Here is the code
return "success";
}
first of all it must be JSON.stringify not json.stringify, second it must be contentType not contenttype third the name of parameters in [WebMethod] must be same as in your ajax data.
in this case schoolName not schoolname.
hope it help you.
I am learning jQuery and trying the following but the parameters are so foreign to me with all the embedded quotes I think that is my problem. Can someone explain the parameters and where quotes go and possibly rewrite my parameters line? (This is a live site to see the required parms).
function AirportInfo() {
var divToBeWorkedOn = '#detail';
var webMethod = "'http://ws.geonames.org/citiesJSON'";
var parameters = "{'north':'44.1','south':'9.9','east':'22.4','west':'55.2','lang':'de'}";
$.ajax({
type: "POST",
url: webMethod,
data: parameters,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg) {
alert(msg.d);
$(divToBeWorkedOn).html(msg.d);
},
error: function(xhr) {
alert(xhr);
alert(xhr.statusText);
alert(xhr.responseText);
$(divToBeWorkedOn).html("Unavailable");
}
});
}
It looks like you are going to have a problem with the same origin policy.
In a nutshell, the policy prevents submitting an AJAX request across pages on different domains.
You should probably be using JSONP for Geonames, as described in the following Stack Overflow post:
JSONP callback fails, need help with javascript/jquery
Apart from that, you wouldn't have needed the single quotes here:
var webMethod = "http://ws.geonames.org/citiesJSON";
Try this way
var divToBeWorkedOn = '#detail';
var webMethod = "'http://ws.geonames.org/citiesJSON'";
var parameters = {'north':'44.1','south':'9.9','east':'22.4','west':'55.2','lang':'de'};
$.ajax({
'type': "POST",
'url': webMethod,
'data': parameters,
'contentType': "application/json; charset=utf-8",
'dataType': "json",
'success': function(msg) {
alert(msg.d);
$(divToBeWorkedOn).html(msg.d);
},
'error': function(xhr) {
alert(xhr);
alert(xhr.statusText);
alert(xhr.responseText);
$(divToBeWorkedOn).html("Unavailable");
}
});
I always use the way below. See if that works for you. I changed your code to be more like I'd do:
var divToBeWorkedOn = '#detail';
var webMethod = "http://ws.geonames.org/citiesJSON";
var parameters = { north:'44.1',south:'9.9', east:'22.4', west:'55.2',lang:'de' };
$.ajax({
type: "POST",
url: webMethod,
data: parameters,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg) {
alert(msg.d);
$(divToBeWorkedOn).html(msg.d);
},
error: function(xhr) {
alert(xhr);
alert(xhr.statusText);
alert(xhr.responseText);
$(divToBeWorkedOn).html("Unavailable");
}
});
i always write parameters like this:
data: "north=33.4&south=3"..... ,