简体   繁体   English

将查询字符串值传递到ASP.NET中的jQuery Ajax调用中

[英]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. 我有一个带单个参数的webMethod,我想通过jQuery Ajax调用。 Now the argument is supposed to be the query string of the url in which I am making the ajax call. 现在,该参数应该是我在其中进行ajax调用的URL的查询字符串。 I don't know how to pass the query string as an argument into the method from the jQuery ajax. 我不知道如何将查询字符串作为参数从jQuery ajax传递到方法中。 My Code is as follows 我的代码如下

C# Method C#方法

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

}

jQuery Ajax 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. 您需要将数据作为URL的一部分传递或将方法更改为GET。 When submitting an aJax post with data you are sending the data as part of the request body (form data). 提交包含数据的aJax帖子时,您会将数据作为请求正文的一部分发送(表单数据)。 This won't be as a query string attribute but part of the content. 这不是查询字符串属性,而是内容的一部分。 Instead change the URL To include the data. 而是更改URL以包括数据。 Example. 例。

Default.aspx/FollowUser?userId=user1

changing to a GET method will force the post parameters to be part of the query string instead. 更改为GET方法将强制将post参数改为查询字符串的一部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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