Quiero llamar a un método web usando jquery ajax get method.but no se está llamando. debajo de mi javascript y codigo
javascript:
function RetrievePassword() { var forgotEmail = $("#txtForgotEmail").val().trim(); //call the ajax method to retrieve the password from the email address provided. $.ajax({ type: "GET", url: "test.aspx/RetrievePassword?email=" + forgotEmail, contentType: "application/json; charset=utf-8", dataType: "json", success: function (result) { alert(result.d); }, error: function () { alert("Error while calling the server!"); } }); }
mi código detrás de la función
[WebMethod] [ScriptMethod(UseHttpGet=true)] public static string RetrievePassword(string email) { //some code }
Puede alguien ayudarme con esto..
Por razones de seguridad, los métodos de página ASP.Net AJAX
solo admiten solicitudes POST
.
El siguiente ejemplo se muestra utilizando la solicitud POST
jQuery
$.ajax({ type: "POST", url: "test.aspx/RetrievePassword", contentType: "application/json; charset=utf-8", dataType: "json", data: '{email:"' + forgotEmail + '"}', success: function (result) { alert(result.d); }, error: function () { alert("Error while calling the server!"); } });
C # Pagemethod
[WebMethod] public static void RetrievePassword(string email) { //some code }
Recuerde utilizar el nombre de variable de datos de publicación ajax como se usa en el argumento de pagemethod. Como es caso-sesitive
prueba esto en tu código:
$.ajax({ url: 'URL', data: "{ 'Keyword': '" + forgotEmail + "'}", dataType: "json", type: "POST", contentType: "application/json; charset=utf-8", success: function (data) { response($.map(data.d, function (item) { console.log(item); return { item; } })) }, error: function (response) { console.log(response.responseText); }, failure: function (response) { console.log(response.responseText); } });
para estar seguro de su código, siempre vaya con console.log();