使用AJAX发出POST请求;不起作用

Make a POST request using AJAX doesn't work

本文关键字:不起作用 请求 POST AJAX 发出 使用      更新时间:2023-09-26

我有一个纯javascript代码,我需要发送一个JSON(可能很大,超过了GET限制……),所以我想使用AJAX进行POST请求。

我的代码是。。。

    <!DOCTYPE html>
    <html>
    <head>
      <meta charset="utf-8" />
      <title>Test POST via AJAX</title>
    </head>
    <body>
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.2/jquery.min.js"></script>
      <script>
        var waypoints = [{
                "options": {
                    "allowUTurn": false
                },
                "latLng": {
                    "lat": 44.911434,
                    "lng": 7.671602
                },
                "name": "Corso Vinovo, Carignano, TO, Piemonte, Italia",
                "_initHooksCalled": true
              }, {
                "options": {
                    "allowUTurn": false
                },
                "latLng": {
                    "lat": 44.910167,
                    "lng": 7.676274
                },
                "name": "Il Tempio del Pane, Corso Cesare Battisti, Carignano, TO, Piemonte, Italia",
                "_initHooksCalled": true
              }, {
                "options": {
                    "allowUTurn": false
                },
                "latLng": {
                    "lat": 44.908034,
                    "lng": 7.675075
                },
                "name": "Banca Intesa, Via Ferdinando Salotto, Carignano, TO, Piemonte, Italia",
                "_initHooksCalled": true
        }];
        alert ('Waypoints --> ' + JSON.stringify(waypoints));
        $.ajax({
          type: "POST",
          url: "NewPage.php",
          contentType: "application/json",
          data: JSON.stringify(waypoints),
          success: function()
           {
            alert("OK ...");
           },
          error: function()
           {
             alert("KO ...")
           }
        })
       alert('After post request ...');
    </script>
   </body>
  </html>

这是NewPage.php 的代码

<?php
    echo 'Waypoints ?????';
    print_r($_POST)
?>

执行代码时,我可以看到我的AJAX代码提醒我"OK",但我看不到任何关于我的NewPage.php 的信息

我对这个东西还很陌生,很抱歉太平庸了。。。。

有什么建议,例如,jsfiddle,替代方案吗?

提前感谢!

Cesare

您应该在php中使用die。

 <?php
       echo json_encode($data_to_pass);
       die;
    ?>

在您的javascript文件中,您应该使用

success: function(data)
           {
            alert(data);
           },

由于您使用的内容类型为application/json,因此您应该从php页面对结果进行json_encode。还要使用exit,否则你会想知道为什么它会返回整个页面,而不仅仅是json数据。

echo json_encode($some_data);
exit();
        $.ajax({
          type: "POST",
          url: "NewPage.php",
          dataType: "json",
          data: {'waypoints':waypoints},
          success: function(resp)
           {
            //alert("OK ...");
           },
          error: function()
           {
             //alert("KO ...")
           }
        })

NewPage.php

            <?php
                echo 'Waypoints ?????';
                print_r($_POST['waypoints']);
            ?>

尝试将data:waypoints更改为data:{mydata:waypoint}。直接传递数组会导致jQuery以一种奇怪的格式传递它。http://api.jquery.com/jQuery.param/

您没有使用ajax成功回调返回的响应

$.ajax({
          type: "POST",
          url: "NewPage.php",
          contentType: "application/json",
          data: JSON.stringify(waypoints),
          success: function(response)
           {
            alert(response);
           },
          error: function(err)
           {
             alert(err)
           }
        })

还有

alert('After post request ...');

它不会在ajax调用完成后执行,它将在发出ajax请求后被完全调用,与完成或挂起无关。所以在成功或错误中做你的事情

将成功回调更改为:

success: function(data)
           {
            alert(data);
           },

一旦AJAX帖子成功完成,您应该会看到警报中PHP页面中的响应内容。