Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
306 views
in Technique[技术] by (71.8m points)

javascript - 如何使用jQuery在GET请求中传递参数(How to pass parameters in GET requests with jQuery)

How should I be passing query string values in a jQuery Ajax request?(我应该如何在jQuery Ajax请求中传递查询字符串值?)

I currently do them as follows but I'm sure there is a cleaner way that does not require me to encode manually.(我目前按照以下方式执行它们,但我确信有一种更简洁的方法,不需要我手动编码。) $.ajax({ url: "ajax.aspx?ajaxid=4&UserID=" + UserID + "&EmailAddress=" + encodeURIComponent(EmailAddress), success: function(response) { //Do Something }, error: function(xhr) { //Do Something to handle error } }); I've seen examples where query string parameters are passed as an array but these examples I've seen don't use the $.ajax() model, instead they go straight to $.get() .(我已经看到了查询字符串参数作为数组传递的示例,但是我见过的这些示例不使用$.ajax()模型,而是直接使用$.get() 。) For example:(例如:) $.get("ajax.aspx", { UserID: UserID , EmailAddress: EmailAddress } ); I prefer to use the $.ajax() format as it's what I'm used to (no particularly good reason - just a personal preference).(我更喜欢使用$ .ajax()格式,因为它是我习惯的(没有特别好的理由 - 仅仅是个人偏好)。) Edit 09/04/2013:(编辑09/04/2013:) After my question was closed (as "Too Localised") i found a related (identical) question - with 3 upvotes no-less (My bad for not finding it in the first place):(在我的问题被关闭后(作为“Too Localized”),我发现了一个相关的(完全相同的)问题 - 3个upvotes no-less(我不喜欢首先找到它):) Using jquery to make a POST, how to properly supply 'data' parameter?(使用jquery进行POST,如何正确提供'data'参数?) This answered my question perfectly, I found that doing it this way is much easier to read & I don't need to manually use encodeURIComponent() in the URL or the DATA values (which is what i found unclear in bipen's answer).(这完全回答了我的问题,我发现这样做更容易阅读,我不需要在URL或DATA值中手动使用encodeURIComponent() (这是我在bipen的答案中发现的不清楚)。) This is because the data value is encoded automatically via $.param() ).(这是因为data值是通过$.param()自动编码的。) Just in case this can be of use to anyone else, this is the example I went with:(为了防止这对任何人都有用,这就是我的例子:) $.ajax({ url: "ajax.aspx?ajaxid=4", data: { "VarA": VarA, "VarB": VarB, "VarC": VarC }, cache: false, type: "POST", success: function(response) { }, error: function(xhr) { } });   ask by HeavenCore translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Use data option of ajax.(使用ajax的数据选项。)

You can send data object to server by data option in ajax and the type which defines how you are sending it (either POST or GET ).(您可以通过ajax中的data选项将数据对象发送到服务器,以及定义发送方式的typePOSTGET )。) The default type is GET method(默认类型是GET方法) Try this(试试这个) $.ajax({ url: "ajax.aspx", type: "get", //send it through get method data: { ajaxid: 4, UserID: UserID, EmailAddress: EmailAddress }, success: function(response) { //Do Something }, error: function(xhr) { //Do Something to handle error } }); And you can get the data by (if you are using PHP)(你可以通过(如果你使用PHP)获取数据) $_GET['ajaxid'] //gives 4 $_GET['UserID'] //gives you the sent userid In aspx, I believe it is (might be wrong)(在aspx中,我相信它(可能是错的)) Request.QueryString["ajaxid"].ToString();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...