weixin_33709219 2018-09-20 18:40 采纳率: 0%
浏览 36

用PHP进行Ajax调用

Im using $.ajax in javascript. I need to get the response from php file. The code in javascript is -

var datavalues = {
                 a: 12,
                 b: 54
                 };


$.ajax({
       type: "POST",
       url: 'http://localhost/example/test.php',
       data: datavalues,
       success: function(response)
       {
       console.log(response);
       $('#label').html(response);
       var responsevalue = response;
       }
       });

and the code in php file is -

$bt = rand(0, 99);
$bt = intval($bt);
echo $bt;

The issue is that it shows the value in the label but value is not coming fine in the responsevalue variable. I need integer value in the responsevalue variable.

Output of console.log(response); statement is -

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Page</title>
</head>
<body>
</body>
</html>68<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Page</title>
</head>
<body>
</body>
</html>

Here 68 should be the value of responsevalue variable.

I hope you are not confused with the above code.

  • 写回答

2条回答 默认 最新

  • 普通网友 2018-09-20 20:25
    关注

    Use jquery param

    var datavalues = {
                     a: 12,
                     b: 54
                     };
    
    
    $.ajax({
           type: "POST",
           url: 'http://localhost/example/test.php',
           data: $.param(datavalues),
           success: function(response)
           {
           console.log(response);
           $('#label').html(response);
           var responsevalue = response;
           }
           });
    

    in PHP file echo $_POST['a'];

    评论

报告相同问题?

悬赏问题

  • ¥15 streamingtool
  • ¥15 MATLAB图像问题
  • ¥20 树莓派5做人脸情感识别与反馈系统
  • ¥15 selenium 控制 chrome-for-testing 在 Linux 环境下报错 SessionNotCreatedException
  • ¥15 使用pyodbc操作SQL数据库
  • ¥15 MATLAB实现下列
  • ¥30 mininet可视化打不开.mn文件
  • ¥50 C# 全屏打开Edge浏览器
  • ¥80 WEBPACK性能优化
  • ¥30 python拟合回归分析