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 网络科学导论,网络控制
  • ¥100 安卓tv程序连接SQLSERVER2008问题
  • ¥15 利用Sentinel-2和Landsat8做一个水库的长时序NDVI的对比,为什么Snetinel-2计算的结果最小值特别小,而Lansat8就很平均
  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同