[英]Unexpected behaviour while decoding strings in PHP (from AJAX POST call)
[英]Ajax and PHP - unexpected behaviour
我正在使用ajax将请求发送到php页面,该页面设置cookie并使用echo函数将消息发送回浏览器。 但是问题是php成功设置了cookie,但是我没有在echo中收到消息。 我已经在控制台上记录了XMLHTTPRequest对象以确认这一点。
这是php代码:
<?php header("Access-Control-Allow-Origin: *");
session_start();
// echo "true"; - If I put the echo here , I get the response.
if(isset($_POST['init'])){
$x00 = universe::decode($_POST['init']);// Just a static function call of object Universe.
$_SESSION['id'] = $x00['id'];
echo "true"; // This echo does not appear in the response.
}
?>
找到了问题,谢谢大家的帮助。
我本来要做的是将json数据字符串化,并作为普通的发帖请求发送并期望它能正常工作,但是由于某种原因,它没有。
我编写了一个函数,该函数将发布请求发送到服务器。
ajax.send(x); //Send is a function of the object ajax , x is the payload.
param = {"id": "qbcd"} //json data
param=JSON.stringify(ajax.param); // converted to string.
ajax.send("init="+param); // This will send a post request to the server.
我以为它将发送init为字符串的post请求,然后可以使用$ _POST ['init']访问该字符串并在其上使用json_decode。 但是由于某种原因,有效负载仅包含经字符串解析的json(已通过Chrome开发工具进行了检查),因此现在我们必须使用$ _POST [0]访问它,即访问post数组的第一个元素,然后对其进行解析。 现在,Post不是关联数组(为什么是idk)。
感谢您的评论:)喜欢stackoverflow。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.