JQuery的,如何通过在GET请求参数 [英] JQuery, How to pass parameters in get requests

查看:131
本文介绍了JQuery的,如何通过在GET请求参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

快速容易的,你对一个jQuery Ajax请求传递查询字符串值preferred的方式,我做他们的内容,但我敢肯定有,不需要我去EN code一个更清洁的方式手动?

  $。阿贾克斯({
    网址:ajax.aspx ajaxid = 4和用户名=+用户名+&放大器; EmailAddress的=EN + codeURIComponent(EmailAddress的),
    成功:函数(响应){
        //做一点事
    },
    错误:函数(XHR){
        //做一些事情来处理错误
    }
});
 

我在哪里见过查询字符串参数传递一个数组,但这些例子我见过不使用 $。阿贾克斯()模式的例子,相反,他们直奔至 $得到(),例如:

  $得到(ajax.aspx,{用户名:用​​户名,EmailAddress的:EmailAddress的});
 

我preFER使用$阿贾克斯()格式,因为它是我已经习惯了(没有什么特别好的理由 - 只是个人的preference)。

修改2013年9月4日:

在我的问题被关闭(如太本地化),我发现了一个相关的(相同的)的问题 - 用3 upvotes没有少(我的坏没有找到它摆在首位):

<一个href="http://stackoverflow.com/questions/3066070/using-jquery-to-make-a-post-how-to-properly-supply-data-parameter">Using jQuery的做出一个帖子,如何正确地提供数据参数?

这回答我的问题很好,我发现,做这种方式是很容易阅读和放大器;我不需要手动使用连接codeURIComponent()的URL或数据值(这是我发现不清楚bipen的答案)。这是因为数据值通过 <$是连接codeD自动C $ C> $。参数() )。万一这可能是使用于其他任何人,这是我去的例子:

  $。阿贾克斯({
    网址:ajax.aspx ajaxid = 4,
    数据: {
        翻:翻,
        VarB:VarB,
        VARC:VARC
    },
    缓存:假的,
    键入:POST,
    成功:函数(响应){

    },
    错误:函数(XHR){

    }
});
 

解决方案

AJAX的使用数据的选项。您可以通过数据发送数据对象到服务器在AJAX选项和键入定义你如何发送它(无论是 POST GET )。默认类型为 GET 方法

试试这个

  $。阿贾克斯({
  网址:ajax.aspx
  类型:获取,//通过GET方法发送
  数据:{ajaxid:4,用户名:用​​户名,EmailAddress的:恩codeURIComponent(EmailAddress的)},
  成功:函数(响应){
    //做一点事
  },
  错误:函数(XHR){
    //做一些事情来处理错误
  }
});
 

您可以通过

获得数据

  $ _ GET ['ajaxid'] //给出4
 $ _GET [用户名] //给你发送用户ID
 

Quick easy one, what’s the preferred way of passing querystring values on a JQuery Ajax request, i 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 querystring parameters are passed as an array but these examples ive seen don’t use the $.ajax() model, instead they go straight to $.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).

Edit 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):

Using jquery to make a POST, how to properly supply 'data' parameter?

This answered my question perfectly, I found that doing it this way is much easier to read & i dont need to manually use encodeURIComponent() in the URL or the DATA values (which is what i found unclear in bipen's answer). This is because the data value is encoded automatically via $.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) {

    }
});

解决方案

Use data option of 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). The default type is GET method

Try this

$.ajax({
  url: "ajax.aspx",
  type: "get", //send it through get method
  data:{ajaxid:4,UserID: UserID , EmailAddress:encodeURIComponent(EmailAddress)},
  success: function(response) {
    //Do Something
  },
  error: function(xhr) {
    //Do Something to handle error
  }
});

And you can get the data by

 $_GET['ajaxid'] //gives 4
 $_GET['UserID'] //gives you the sent userid

这篇关于JQuery的,如何通过在GET请求参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆