繁体   English   中英

I'm using AJAX to post an object from javascript to php within the same page and I'm getting the entire html page instead of the object

[英]I'm using AJAX to post an object from javascript to php within the same page and I'm getting the entire html page instead of the object

instead of getting the object as the responce data I'm getting the entire html page (the page I'm posting to) therefore I cannot retrieve the object in the php script

                     //this is an example of the object not the actual abject
                     var data={lat:"1.098", lng:"31", city:"durban"};

                     $.ajax({
                              type: "POST",
                              url: "/webapp/index.php/",
                              data: data,
                              async:true,
                          
                              cache: false,
                              success: function(data)
                              {
                                alert(data.status);
                                console.log(data);
                              },
                                error: function (xhr, status, error) {
                                alert(status);
                                alert(xhr.responseText);
                              }
                          });

由于服务器将消耗<?php /*some PHP scripts */但返回以下<.DOCTYPE html><html> etc...作为响应 -你需要exit

index.php

<?php
 if ($_SERVER['REQUEST_METHOD'] === 'POST') {
   // Handle POST requests here 
   echo "HELLO WORLD!";
   exit;  // <<< You need to exit here!
 }
?>
<!DOCTYPE html>
<html lang="en">
<head><!-- head stuff --></head>
<body>
    
  SOME FORM

  <script>
     // Some AJAX to "self" (index.php)
     // success response: "HELLO WORLD!"
  </script>
</body>
</html>

甚至更短:

exit("HELLO WORLD!");

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM