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 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换