我正在使用 AJAX 在同一页面中将一个对象从 javascript 发布到 php,并且我正在获取整个 html 页面而不是对象



而不是获得对象作为响应数据,我得到整个html页面(我张贴的页面),因此我无法检索对象在php脚本

//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!");

最新更新