简体   繁体   中英

Pass Query String Value into jQuery Ajax Call in ASP.NET

I have a webMethod with a single argument I am trying to call via jQuery Ajax. Now the argument is supposed to be the query string of the url in which I am making the ajax call. I don't know how to pass the query string as an argument into the method from the jQuery ajax. My Code is as follows

C# Method

[WebMethod]
public static string FollowUser(string userId)
{
   //Code to follow user
   //returns a string value

}

jQuery Ajax

$(document).ready(function() {
            $("#btnFollow").click(function() {
                $.ajax({
                    type: "POST",
                    url: "Default.aspx/FollowUser",

                    data: //how do I pass the query string here 

                    contentType: "application/json; charset=utf-8", datatype: "json",
                    context: this,
                    success: function(msg) {
                        //I'm doing some stuff here
                        }

                    },

                    error: function(response) {
                        alert(response.d);
                    }
                });
            });
        });

Someone help me out. Thanks.

You need to pass the data as part of the the URL or change the method to GET. When submitting an aJax post with data you are sending the data as part of the request body (form data). This won't be as a query string attribute but part of the content. Instead change the URL To include the data. Example.

Default.aspx/FollowUser?userId=user1

changing to a GET method will force the post parameters to be part of the query string instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM