【发布时间】:2012-09-04 01:41:08
【问题描述】:
我正在使用 Ajax 向我的 php 文件发送 HTTP POST 请求,但没有得到想要的结果。 $_POST 和 $_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;
我得到的响应是一个空数组。
- 我测试了输入(见上文);
- 我知道 Ajax 调用本身会成功,如果我将 PHP 示例中的第二行替换为
json_encode(array('success' => true));我会在我的页面中恢复 - 所以 URL 是正确的。 - 我使用 GET 和 POST 对其进行了测试,结果相似。
【问题讨论】:
-
你能写出 ko.toJSON([this.name, this.description, this.pages]) 的输出吗?
-
可以在 KnockoutJS 中使用 this 操作符吗?在这一行中:ko.toJSON([this.name, this.description, this.pages])。有一些关于 this 和 self 的文档。
-
["Name","Description",[{"title":"Page 1","selectedPageStyle":"Header"}]] -
json应该使用控制台,PHP中不需要设置header。
-
我们不必为此使用 dataType : 'json' 吗?
标签: php javascript jquery