Ajax传递两个参数

时间:2018-04-11 04:19:41

标签: ajax

这是我将两个变量传递给php文件的ajax代码。

var developer = $('#developerSelect').val();
      var level = $('#userLevelSelect').val();
      console.log(developer);
      console.log(level);

     $.ajax({
            url: 'ajax-GenerateUserbyLevelReport.php',
            type: 'post',
            data: {developerSelected : developer,userlevel : level },
            dataType: 'json',
            success:function(response){
              console.log(response);
            }

          })

我将两个变量传递给php文件,其语法如下: -

  

数据:{developerSelected:developer,userlevel:level},   


ajax-GenerateUserbyLevelReport.php: -

    <?php
    require_once 'dbconfig.php';

    $developer = $_POST['developerSelected'];
    $userLevel = $_POST['userlevel'];

    // this query is to get the developer id 
$DevQuery="SELECT id AS `ID` FROM pams_developer WHERE developer_name=:name";
$Dev_ID = ($GetReport->GetID($DevQuery,$developer));

//this query is the get all the user level id 
$UserLevelQuery="SELECT id AS `ID` FROM pams_user_levels WHERE description=:name";
$Level_ID = ($GetReport->GetID($UserLevelQuery,$userLevel));


    $UserLevelQuery = "SELECT `pams_users`.display_name AS Name ,`pams_user_levels`.description,`pams_users`.datecreated AS DateCreated,last_date AS LastLogin FROM pams_users 
    JOIN pams_developer ON `pams_users`.developer_id=`pams_developer`.id AND `pams_developer`.id=:dev_id 
    JOIN pams_user_levels ON `pams_user_levels`.id=`pams_users`.user_level AND `pams_user_levels`.id=$Level_ID
    ORDER BY `pams_user_levels`.description ASC";
    $UserbyUserLevel= $GetReport->GetData($UserLevelQuery,$Dev_ID);
    echo json_encode($UserbyUserLevel,JSON_HEX_APOS);
        //json_encode data echo here
        ?>

但问题是console.log(响应)什么也没有返回。 任何人都可以帮我解决我的问题吗?

2 个答案:

答案 0 :(得分:0)

使用curl进行调试,例如

Curl -v -X POST 'http://server/server.script.php' -d '{"developerSelected":"Jhon","user level":"24"}'

答案 1 :(得分:0)

您的dataType设置为json,但您的数据不是json格式,因此只需删除dataType即可使用

   $.ajax({
            url: 'ajax-GenerateUserbyLevelReport.php',
            type: 'post',
            data: {developerSelected : developer,userlevel : level },
            //dataType: 'json', // just remove this line
            success:function(response){
              console.log(response);
            }

   });

如果您仍想使用json,则需要使用JSON.stringify()来更改数据参数:

   var dataObj = {developerSelected : developer,userlevel : level };
   $.ajax({
            url: 'ajax-GenerateUserbyLevelReport.php',
            type: 'post',
            data: JSON.stringify(dataObj),
            dataType: 'json', 
            success:function(response){
              console.log(response);
            }

   });