使用jQuery AJAX发送多个数据参数 [英] Sending multiple data parameters with jQuery AJAX

查看:188
本文介绍了使用jQuery AJAX发送多个数据参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发送一个Ajax请求的PHP文件,如下所示:

I am sending an ajax request to a php file as shown here:

function checkDB(code, userid)
{

  $.ajax({
  type: "POST",
  url: "<?php bloginfo('template_url'); ?>/profile/check_code.php",
  data: 'code='+code+'userid='+userid,
  datatype: "html",
  success: function(result){

       if(result == 0)
        {
            $('#success').html( code + ' has been redeemed!');
            // alert('success');//testing purposes
        }
        else if(result == 2)
        {
            $('#err').html(  code + ' already exists and has already been redeemed....');
            //alert('fail');//testing purposes
        }else if(result == 1){
            $('#err').html(  code + ' redeem code doesnt exist');      
        }

        alert(result);      
      }
  })

}

这是发出呼吁提交,像这样的功能:

This is sent calling the function on submit, like so:

<form method="post" class="sc_ajaxxx" id="sc_add_voucherx" name="sc_ajax"  
     onsubmit="checkDB(document.sc_ajax.sc_voucher_code.value, <?php echo $user_id ?>); return false;">
</form>

现在的问题是,用户ID PHP变量是没有得到通过AJAX发送到check_ code.php页面。至少我不能似乎呼应ID返回页面。

The problem is that the user id php variable is not getting sent to the check_code.php page by ajax. or at least I cant seem to echo the id back to the page.

这是传递多个值到服务器端页面的正确方法是什么?如果没有用户ID掠过,它工作正常,只是越过code。

Is this the correct way of passing multiple values to a server-side page? Without the userid passing over, it works fine just passing over the code.

感谢球员:)

推荐答案

下面是如何 POST 数据应该被格式化:

Here is how POST data should be formatted:

key1=value1&key2=value2&key3=value3

在你的情况(注意&安培; 作为分隔符):

In your case (note the & as a separator):

'code=' + code + '&userid=' + userid

但jQuery不会为你,如果你指定的对象数据:

But jQuery does that for you if you specify your data as an object:

data: { code: code, userid: userid }

这篇关于使用jQuery AJAX发送多个数据参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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