My code behind:
[WebMethod]
public bool accountExists(string username, string password) {
//code...
}
My jquery:
$.ajax({
type: "POST",
url: "MyPage.ascx/accountExists",
data: JSON.stringify({ username: txtUsername.val(), password: txtPassword.val()}),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg) {
alert(msg.d)
},
error: function(msg) {
alert("ERROR: " + msg.d)
}
});
I always reach the alert where it says "ERROR: " + msg.d.
MyPage.ascx is located in a folder "Controls", so I have tried to set the url: "Controls/MyPage.ascx/accountExists" without any change.
ASP.NET AJAX Page Methods are intended to run inside of .aspx pages and not .ascx user controls.
Move your WebMethod logic into an .aspx page and update the AJAX call via jQuery.
Related
Salam aleykum, i'm trying here to call an ajax server-side method using the mappage route but it always says: POST 404 (Not found)
here is the code c#:
[System.Web.Services.WebMethod]
public static bool RemovePhotofromAlbum(string list_photos_hotel)
{
.....
return true;
}
and here the jquery code should like :
function RemovePhotofromAlbum(list_photos_hotel) {
$.ajax({
type: "POST",
url: $(location).attr('pathname') + "/RemovePhotofromAlbum",
data: '{list_photos_room_type: "' + list_photos_hotel + '" }',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
....
},
failure: function (response) {
alert(response.d);
}
});
}
Without using the mappage route it's working.
but here i want to use the mappage route
i know that there is a problem with the URL in the ajax method but i don't know how to fix it.
any help would be appreciated.
:)
If it's impossible just tell me
$(location).attr('pathname')
This line is used for add attribute in DOM
So if you want to save baseurl,Keep it in web.config, hidden field or any other js file and than use it
You should use $(location).attr('href') and not $(location).attr('pathname')
and you have an error with your parameter name it should be 'list_photos_hotel' and not 'list_photos_room_type'
try this :
function RemovePhotofromAlbum(list_photos_hotel) {
$.ajax({
type: "POST",
url: $(location).attr('href') + "/RemovePhotofromAlbum",
data: '{list_photos_hotel: "' + list_photos_hotel + '" }',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
},
failure: function (response) {
alert(response.d);
}
});
}
assuming you run the script from the same aspx page your server method runs.
Edit :
Because you use map route, you get 404. you should pass the physical location.
Your method is in the path : Manage/admin_2/index.aspx :
function RemovePhotofromAlbum(list_photos_hotel) {
$.ajax({
type: "POST",
url: "/Manage/admin_2/index.aspx/RemovePhotofromAlbum",
data: '{list_photos_hotel: "' + list_photos_hotel + '" }',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
},
failure: function (response) {
alert(response.d);
}
});
}
I'm building a Web Application in which I'm trying to call a WebMethod in a WebForm, I've tried every single page in google but I'm still getting nothing. This is an example of the Jquery Ajax Call
$.ajax({
type: "Post",
url: "Default.aspx/Return",
data: {dato:'Hello'},
contentType: "application/json; chartset:utf-8",
dataType: "json",
success:
function (result) {
if (result.d) {
alert(result.d);
}
},
error:
function (XmlHttpError, error, description) {
$("#grdEmpleados").html(XmlHttpError.responseText);
},
async: true
});
And this is the WebMethod in the codebehind
[WebMethod]
[ScriptMethod(ResponseFormat=ResponseFormat.Json)]
public static string Return(string dato)
{
return dato;
}
You can't access a Static method this way. Remove the "Static" reference and it will work. Also, like someone else said - do not use that as your method name "Return".
[WebMethod]
[ScriptMethod(ResponseFormat=ResponseFormat.Json)]
public string Return(string dato)
{
return dato;
}
I think, on your success event the function with result is used, which is a string and u are trying to access property named d assuming result is an object.
use of only alert(result);
User F12 tool to debug and find your error.
Make sure that you have enabled page methods in your ScriptManager element:
<asp:ScriptManager ID="scriptManager" runat="server" EnablePageMethods="true" />
and your method
$.ajax({
type: "Post",
url: '<%= ResolveUrl("~/Default.aspx/Return") %>',
data: {dato:'Hello'},
contentType: "application/json; charset=utf-8",
dataType: "json",
success:
function (result) {
alert(result);
},
error:
function (XmlHttpError, error, description) {
$("#grdEmpleados").html(XmlHttpError.responseText);
},
async: true
});
Try this
var url = window.location.pathname + "/Return";
$.ajax({
type: "Post",
url: url,
data: {dato:'Hello'},
contentType: "application/json; charset=utf-8",
dataType: "json",
success:
function (result) {
alert(result.d);
},
error:
function (XmlHttpError, error, description) {
$("#grdEmpleados").html(XmlHttpError.responseText);
},
async: true
});`
I have an AJAX call to a function as follows:
$("#<%=gridview.ClientID%> tr:has(td)").click(function(e) {
var id = $(this).children("td:eq(3)").find(':input').val();
$.ajax({
type: "POST",
url: "Docs.aspx/BtnTest",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
if (msg.d == 'Success') {
window.open('/view.aspx?Id=' + id, '_blank');
}
}
});
e.preventDefault();
});
As you can see in above code on success I am opening another page with id as querystring. How can I call Colorbox or any other lightbox plugin to open this in iframe?
Try this:
$.colorbox({ href: 'your link goes here', iframe:true, width:"80%", height:"80%"});
Same problem:
add colorbox to dynamicly created item with url content
I'm using JScript + ASP.NET. I got a form with 2 inputs (user and password) and a button. What I'm trying to do is to:
1- Fire a click event
2- Look inside a database if the user exist
3- Give back the answer
4- If the answer is true, POST some data to an other page AND redirect to it.
I first tried to do this with ASP.NET. To POST data with ASP.NET I need to use PostBackUrl property, but the problem is that PostBackUrl ignore my click event.
I then tried to do this with jscript. On my click event (jquery), I use $.ajax to POST data to access my database, give the answer back in json...and I'm stuck there. In both method, I'm stuck at point 4.
ASP.NET
protected void SignIn_OnClick(object sender, EventArgs e)
{
Clients client = (Clients)clientDAO.getUsername(text1.Text, password2.Text);
if (client != null)
{
Session.Add("SessionNoClient", "1272");
Session.Add("CurrentQuote", "-1");
Session.Add("UnitSystem", "0");
Session.Add("SessionAdministrator", "0");
//How to redirect with POST here
}
}
JScript:
$("#m_bLogin").click(function () {
var username = $("#text1").val();
var password = $("#password2").val();
var form = $("#formClient");
$.ajax({
url: '../../Class/LoginAjax.asmx/GetLoginInformation',
data: "{ 'Name':'" + username + "','Password':'" + $("#password2").val() + "'}",
dataType: "json",
type: "POST",
contentType: "application/json; charset=utf-8",
success: function (data) {
//My Json returns {"'Name':'Bob','Password':'1234'} and I'm not able to access Name or Password property. I tried data.d, data.d.Name, eval(data.d.Name) etc...
form.submit();
},
error: function (XMLHttpRequest, textStatus, error) {
alert(error);
}
});
});
You could do something like that:
$.ajax({
url: '../../Class/LoginAjax.asmx/GetLoginInformation',
data: "{ 'Name':'" + username + "','Password':'" + $("#password2").val() + "'}",
dataType: "json",
type: "POST",
contentType: "application/json; charset=utf-8",
success: function (data) {
//My Json returns {"'Name':'Bob','Password':'1234'} and I'm not able to access Name or Password property. I tried data.d, data.d.Name, eval(data.d.Name) etc...
form.submit();
},
error: function (XMLHttpRequest, textStatus, error) {
alert(error);
}
}).done(function() {
window.location.href = "YourNewPage.aspx";
});
I have some ajax functions on my default.aspx that I use for saving data to my db. I have the form on my web control. When I try calling the function from my button which resides on the web control. I get an error.
I need to call that function from my registerform.ascx. How do I do that from within the registerform code behind?
function ShowAvailability() {
$.ajax({
type: "POST",
url: "Default.aspx/CheckEmail",
data: '{usermail: "' + $("#<%=subs_email.ClientID%>")[0].value + '" }',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: OnSuccess,
failure: function(response) {
alert(response);
}
});
use
ButtonID.Attributes.Add("onclick", "ShowAvailability()");
if this wont work
provide the detail of the error u r getting.
It seems I was doing it all wrong.
This code seems to be working:
OnClientClick="return ShowAvailability()"
Initially I had onClick