gpt4 book ai didi

json - index.php 没有收到 Backbone Post 发送的 JSON

转载 作者:行者123 更新时间:2023-12-01 01:13:36 25 4
gpt4 key购买 nike

我一直在尝试了解 Backbone 如何工作以及如何与后端代码通信,但我遇到了无法接收发送到我的 php 文件的 JSON 的问题。

代码如下:HTML:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<link href='http://fonts.googleapis.com/css?family=Abel' rel='stylesheet' type='text/css' />
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Understanding Backbone</title>
<style type="text/css">
body { padding: 0; margin: 0; background-color: #fff; }

h2 { font-family: Abel, sans-serif; margin: 0; padding: 0 0 5px 0;}
input { background-color: #ddd; border: 0; }
input:active { background-color: #bbb; }

#new-status { margin: 20px; padding: 20px; background-color: #67A9C3; }
#statuses { margin: 20px; padding: 20px; background-color: #92B456; }

</style>
</head>

<body>

<div id="new-status">
<h2>New monolog</h2>
<form>
<textarea id="status" name="status"></textarea>
<br />
<input type="submit" value="Post" />
</form>
</div>

<div id="statuses">
<h2>Monologs</h2>
<ul></ul>
</div>
<script src="js/jquery-min.js"></script>
<script src="js/underscore.js"></script>
<script src="js/backbone.js"></script>
<script src="js/main.js"></script>
</body>
</html>

JS:

var Status = Backbone.Model.extend({
url: 'api/index.php'
});

var Statuses = Backbone.Collection.extend({
model: Status
});

var NewStatusView = Backbone.View.extend({
events: {
"submit form": "addStatus"
},

initialize: function(options) {
this.collection.on("add", this.clearInput, this);
},

addStatus: function(e) {
e.preventDefault();

this.collection.create({ text: this.$('textarea').val() });
},

clearInput: function() {
this.$('textarea').val('');
}
});

var StatusesView = Backbone.View.extend({
initialize: function(options) {
this.collection.on("add", this.appendStatus, this);
},

appendStatus: function(status) {
this.$('ul').append('<li>' + status.escape("text") + '</li>');
}
});

$(document).ready(function() {
var statuses = new Statuses();
new NewStatusView({ el: $('#new-status'), collection: statuses });
new StatusesView({ el: $('#statuses'), collection: statuses });
});

索引.php:

<?php

echo(var_dump($_POST));

?>

这是我得到的响应:

数组(0){

我一直为此伤脑筋,所以请帮助!

最佳答案

在对 stackoverflow(很棒的社区 btw)进行更多研究后,我发现 Backbone 不会直接发送帖子或到达 RESTful api,或者任何可能的代码隐藏,而是一组 header .所以你必须四处查看 $_SERVER 全局并找出正在请求的内容。您将能够在 $_SERVER["REQUEST_METHOD"] 中找到您的请求,而不是执行 switch/case 来决定您要对该请求执行的操作。正在发送的数据(在主干的情况下总是一个 JSON 字符串)在 HTTP 主体中,为了把它取出来,我使用了 file_get_contents('php://input'),并对 JSON 进行解码,以便 php 可以使用它.

<?php 
$requestMethod = $_SERVER["REQUEST_METHOD"];
switch ($requestMethod)
{
case 'POST': $data = json_decode(file_get_contents('php://input'), true);
echo $data;
break;
}
?>

@orangewarp,我真的很想在不使用 RESTful php 框架的情况下了解幕后发生的事情。

关于json - index.php 没有收到 Backbone Post 发送的 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12162132/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com