weixin_33727510 2017-07-29 11:20 采纳率: 0%
浏览 36

如何处理来自php POST的Ajax响应?

以下是我的Ajax代码:

 $.ajax({
    url: url,
    type: type,
    data: {data:cart},
    success: function(response){
        console.log(response);
     },
     error: function(error){
     throw new Error('Did not work');
      }
    });

我的问题是:当我检查控制台时,响应的是XML文档,而不是cart的POST数据。我试图使用dataType:'text',但这只是在URL中提供代码,而不是POST数据。当我使用dataType:'json'时,我仍然可以得到XML文档。

我的php代码是:

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
<title>Send.php</title>
</head>
<body>

<?php
  print_r($POST);
?>

</body>
</html>

问题可能出在哪里呢?或者我需要添加什么吗?

  • 写回答

3条回答 默认 最新

  • weixin_33724659 2017-07-29 11:28
    关注

    First thing you don't include that port of code which explain the source of cart which you pass to the server but I think if you write it in a good form and there is no mistake it is an XML data type

    1- at server side you can do this

    A- convert your data to json format with this function

    $POST=json_encode($POST);
    print_r($POST);
    

    B- convert to array

     $POST=json_decode(json_encode($POST));
    print_r($POST):
    

    2- at client side before send data to the server you can use one of any third party script which work on converting xml to json there are more on github.

    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘