Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/461.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ajax POST请求没有';我无法理解PHP_Php_Javascript_Jquery - Fatal编程技术网

Ajax POST请求没有';我无法理解PHP

Ajax POST请求没有';我无法理解PHP,php,javascript,jquery,Php,Javascript,Jquery,我正在用Ajax向我的php文件发出一个HTTP POST请求,但没有得到期望的结果$_POST和$\u GET均为空。我想我忽略了什么,但我不知道是什么 以下是我启动请求的代码: this.save = function() { alert(ko.toJSON([this.name, this.description, this.pages])); $.ajax("x", { data: ko.toJSON([this.name, this.descripti

我正在用Ajax向我的php文件发出一个HTTP POST请求,但没有得到期望的结果$_POST和$\u GET均为空。我想我忽略了什么,但我不知道是什么

以下是我启动请求的代码:

this.save = function() {

    alert(ko.toJSON([this.name, this.description, this.pages]));
    $.ajax("x", {
        data: ko.toJSON([this.name, this.description, this.pages]),
        type: "post", contentType: "application/json",
        success: function(result) { alert(result) },
        error : function(jqXHR, textStatus, errorThrown) { alert(textStatus + errorThrown)}
    });
};
注意,我在第3行警告JSON。JSON是正确的,因此第5行的输入是有效的

我的PHP测试方法:

header('Content-type: application/json; charset=utf-8');
echo json_encode(array_merge($_POST, $_GET));
exit;
我得到的响应是一个空数组

  • 我测试了输入(见上文)
  • 如果我用
    json_encode(array('success'=>true))替换PHP示例中的第二行,我知道Ajax调用本身成功了我在我的页面中得到了它-因此URL是正确的
  • 我用GET和POST对它进行了测试,得到了类似的负面结果

    • 您正在发送一个JSON请求,这就是为什么$\u POST和$\u GET都是空的。尝试按如下方式发送数据:

      $.ajax("x", {
          data: { data: [this.name, this.description, this.pages] },
          type: "post", 
          success: function(result) { alert(result) },
          error : function(jqXHR, textStatus, errorThrown) { alert(textStatus + errorThrown)}
      });
      
      现在查看内部
      $\u POST[“数据”]

      或者,如果需要使用JSON请求,则需要将其反序列化回PHP文件:

      $.ajax("x", {
          data: { data: ko.toJSON([this.name, this.description, this.pages]) },
          type: "post", 
          success: function(result) { alert(result) },
          error : function(jqXHR, textStatus, errorThrown) { alert(textStatus + errorThrown)}
      });
      
      然后解码:

      $json = $_POST['json'];
      $data = json_decode($json);
      
      如果您想在帖子正文中发送纯JSON请求:

      $.ajax("x", {
          data: ko.toJSON([this.name, this.description, this.pages]),
          type: "post", 
          contentType: 'application/json',
          success: function(result) { alert(result) },
          error : function(jqXHR, textStatus, errorThrown) { alert(textStatus + errorThrown)}
      });
      
      然后:

      $data = json_decode(file_get_contents("php://input"));
      

      请注意
      php://input
      是一个只读流,允许您从请求正文中读取原始数据。

      您可以编写ko.toJSON([this.name,this.description,this.pages])的输出吗?您可以在KnockoutJS中使用this操作符吗?在这一行中:ko.toJSON([this.name,this.description,this.pages])。这里有一些关于这个和self的文档。
      [“Name”,“Description”,“title:“Page 1”,“selectedPageStyle:“Header”}]]
      你应该使用json控制台,不需要在PHP中设置Header。我们不需要使用数据类型:“json”吗?我想把它作为json发送。在我使用的示例中(参见此处),JSON是使用POST发送的。为什么我不能将JSON和POST结合起来?我第一次听说tbh..你可以组合,看看我的第二个例子。由于某种原因,我现在根本没有得到任何结果。(我显然更改了url。)url是正确的,但会触发成功警报或错误警报。。。我将尝试调试。您使用3种方法中的哪一种?试试FireBug。这在Opera中是个问题,它在Firefox中确实有效。非常感谢!我会找出歌剧的问题所在。我现在用第三个。