0

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.

1
  • you should also have an error block in your ajax call to see what the error is. That should give you an idea about whats missing. Commented Oct 21, 2016 at 19:36

3 Answers 3

1

You want to convert a JavaScript value to a JSON using JSON.stringify() before posting data.

Default.aspx

<%@ 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>

DetailView.aspx.cs

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;
        }
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

data: "{ \"rID\": \"" + result + "\" }"
data should be on this format data: "{ \"rID\": \"" + result + "\" }"
You do not need to place quotes. data: {id: result }; should work (assuming result is a variable name) or post sample string first data: {id: "test" }; for testing. FYI: If you rename property from pid to to id, you will also have to rename parameter name to id in Test method.
1

From your ajax Method Declaration i.e

  1. 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;
  1. You can send params to your Ajax as follows

    var params = "{'pid:' " + result + "}";

Replace that variable in your AJax calling as follows

   data: params

Comments

0

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()]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.