Reputation: 7
I do have two page. One is Default.aspx and another one is DetailView.aspx. What I want to do is that I want to redirect page from Default.aspx to detailView.aspx using ajax call and I want to pass one value also. I have done something but it is not calling function that is defined into class.
I am calling this function from Default.aspx webfile
$.ajax({
type: 'POST',
url: 'DetailView.aspx/Test',
contentType: "application/json; charset=utf-8",
dataType: "json",
data: '{pid:' + result + '}',
success: function (data) {
}
});
this is class file of DetailView.aspx
[WebMethod(EnableSession = true)]
public static string Test(string pid)
{
return " ";
}
I was debugging this function but it is not calling this function at all when ajax being called.
Upvotes: 0
Views: 1230
Reputation: 8271
From your ajax Method Declaration i.e
URl Part in your Ajax Calling this one :
url: 'DetailView.aspx/Test'
I'm assuming that Your are Using the FriendlyURL
.
So in your RouteConfig.cs
please comment this line
settings.AutoRedirectMode = RedirectMode.Permanent;
You can send params to your Ajax as follows
var params = "{'pid:' " + result + "}";
Replace that variable in your AJax
calling as follows
data: params
Upvotes: 1
Reputation: 62260
You want to convert a JavaScript value to a JSON using JSON.stringify() before posting data.
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="DemoWebForm.Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<button type="button" onclick="ajaxPostData();">Post Data to Detail View</button>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript">
function ajaxPostData() {
var data = { pid: "One" };
$.ajax({
type: "POST",
url: '<%= ResolveUrl("~/DetailView.aspx/Test") %>',
data: JSON.stringify(data),
contentType: "application/json",
success: function (msg) {
console.log(msg.d);
}
});
}
</script>
</form>
</body>
</html>
using System.Web.Services;
namespace DemoWebForm
{
public partial class DetailView : System.Web.UI.Page
{
[WebMethod(EnableSession = true)]
public static string Test(string pid)
{
return "Hello " + pid;
}
}
}
Upvotes: 1
Reputation: 191
Make sure that you have enabled ajax call in the webservice, to do so add this line before defining the webservice class
[System.Web.Script.Services.ScriptService()]
Upvotes: 0