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") %>',
Related
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 have a simple set up.
Jquery:
$.ajax({
url: "/MyApp/MyHandler.ashx/MyMethod",
success: function(result) {
alert("sucess");
},
error: function() {
alert('Error');
}
});
and web method:
[System.Web.Services.WebMethod]
public static void MyMethod(){
new AnotherClass(null).AnotherMethod(null, null);
}
problem is success alert is called but break point is not hit inside MyMethod.
I had the same issue and this is what I ended up having to do:
$.ajax({
url: _url,
data: '',
dataType: 'json',
contentType: 'application/json',
type: 'POST',
success: function(result) {
alert("sucess");
},
error: function() {
alert('Error');
}
});
My first attempt left out the data, dataType and contentType; Only when I set contentType: 'application/json' with an empty string (data: '') did it end up working. What a headache - hope this helps someone else!
In my case, the issue was in RoutingConfig.So, sort that in App_Start folder, within RouteConfig,commented out the following line
//settings.AutoRedirectMode = RedirectMode.Permanent;
I have an Ajax request that's returning a 404. Here is the JavaScript:
function rowClicked(id) {
console.log(id);
$.ajax({
type: "POST",
url: "Details.aspx/GetComment",
data: { "id": id },
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (result) {
console.log(result);
}
});
}
And here is the C# method in Details.aspx.cs - the code behind Details.aspx
[WebMethod()]
public static string GetComment(int id)
{
return "Test";
}
Could anyone offer any insight as to why I might be getting a 404 response?
As most of the people stated, your url might be wrong.
Try to use it like this , maybe it will solve the issue :
<%=ResolveUrl("~/Details.aspx/GetComment") %>
Hope this helps.
I want to call a function from my code behind using javascript. I used the below code:
function fnCheckSelection() {
some script;
window["My"]["Namespace"]["GetPart"](null);
}
...where "GetPart" is the function name. However, this is not working. Please help me on this.
in JavaScript:
document.getElementById("btnSample").click();
Server side control:
<asp:Button runat="server" ID="btnSample" ClientIDMode="Static" Text="" style="display:none;" OnClick="btnSample_Click" />
C#
protected void btnSample_Click(object sender, EventArgs e)
{
}
It is easy way though...
You can do this by an ajax call
this is a jquery example:
$.ajax({
type: "POST",
url:"~/code_behind.aspx/Method",
data: dataPost,
contentType: "application/json; charset=utf-8",
dataType: "json",
....
});
here is api documentation
and in code behind
[WebMethod]
public static yourType Method (Params){}
or you can add a hidden button inside updatePanel, and invoke the click event using js. ('#<%=ID.ClientID%>').click();
It will invoke the OnClientClick if it exists then your codeBehind fucntion.
try this
Your code behind function
[WebMethod]
public static void GetPart() {
//your code goes here
}
.
Javascript
$(document).ready(function () {
$("#btnname").click(function () {
$.ajax({
type: "POST",
url: "/youraspxpagename.aspx/GetPart",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg)
{
}
});
});
});
Using ajax, you can call a codebehind function from javascript using JQuery:
function : fnCheckSelection(){
$.ajax({
cache: false,
url: "/GetPart"
type: "POST",
success: function (result) {
},
error: function (msg) {
}
});
}
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