Javascript 如何使用jQuery.post将JSON数据发送到php脚本?
这在我的javascript代码中。变量数据存储是一个有效的JSON对象,JSON.stringify工作正常-我使用console.log()进行了测试 我是jQuery新手,有一段时间没有编写CGI应用程序,所以我可能在这里做了一些不正确的事情Javascript 如何使用jQuery.post将JSON数据发送到php脚本?,javascript,php,jquery,json,ajax,Javascript,Php,Jquery,Json,Ajax,这在我的javascript代码中。变量数据存储是一个有效的JSON对象,JSON.stringify工作正常-我使用console.log()进行了测试 我是jQuery新手,有一段时间没有编写CGI应用程序,所以我可能在这里做了一些不正确的事情 jQuery.post("taskmanager.php", JSON.stringify(dataStore), function (returnData) { alert(returnData); }); php脚本与我的其他文件(
jQuery.post("taskmanager.php", JSON.stringify(dataStore), function (returnData) {
alert(returnData);
});
php脚本与我的其他文件(taskmanager.html、taskmanager.css、taskmanager.js)位于同一文件夹中。此文件夹位于我的Windows7文件夹E:\JamesLaderoute\MySoftware\WebApp\TaskManager\code
我不确定要将什么放入taskmanager.php脚本中。我想我可以做一些简单的事情,比如:
<?php
$data = $_GET["data"];
echo "done";
?>
PHP代码:
<?php
$contents = file_get_contents('php://input');
$data = json_decode( $contents );
file_put_contents( "taskdata.json", "data=".$contents );
echo "<br>done</br>";
?>
使用jQuery,您可以使用POST协议将数据发送到端点。下面的例子
$.ajax({
url : 'taskmanager.php',
type : 'POST',
data : JSON.stringify(dataStore),
success : function(res) {
// Successfully sent data.
console.log(res);
},
error: function(err) {
// Unable to send data.
console.log(err);
})
});
您也可以使用$.post方法,这只是一个首选项。$。ajax提供了一些额外的灵活性,以防您可能希望更新协议以获取,而不是post或许多其他实例
这里有更多信息:使用jQuery,您可以使用POST协议将数据发送到端点。下面的例子
$.ajax({
url : 'taskmanager.php',
type : 'POST',
data : JSON.stringify(dataStore),
success : function(res) {
// Successfully sent data.
console.log(res);
},
error: function(err) {
// Unable to send data.
console.log(err);
})
});
您也可以使用$.post方法,这只是一个首选项。$。ajax提供了一些额外的灵活性,以防您可能希望更新协议以获取,而不是post或许多其他实例
这里有更多信息:我通常使用以下内容将JSON发布到php
$.post("publishdata.php", {
dataArray: arrayOfStuff,
meetingTitle: meetingTitle,
meetingDate: meetingDate
},
function(data, status, response) {
console.log(response);
});
我通常使用以下内容将JSON发布到php
$.post("publishdata.php", {
dataArray: arrayOfStuff,
meetingTitle: meetingTitle,
meetingDate: meetingDate
},
function(data, status, response) {
console.log(response);
});
jQuery ajax数据的默认
内容类型
是应用程序/x-www-form-urlencoded
,在php中使用$\u GET
、$\u REQUEST
和$\u POST
非常容易,并且不需要像json那样进行字符串化
将对象直接传递给$.post
(或任何$.ajax方法),jQuery将对其进行内部编码
var data ={foo:'bar'};
$.post('taskmanager.php', data, function(resp){
console.log(resp);
})
php
如果只想使用json,则需要设置适当的内容类型
$.ajax({
url:'...',
method:'post',
contentType:'application/json',
dataType:'json',
data: JSON.stringify(data)
}).done(function(resp){
console.log(resp);
});
然后在php中使用:
$data = json_decode(file_get_contents('php://input'));
$data['foo'] ='Some new value';
echo json_encode($data);
jQuery ajax数据的默认
内容类型
是应用程序/x-www-form-urlencoded
,在php中使用$\u GET
、$\u REQUEST
和$\u POST
非常容易,并且不需要像json那样进行字符串化
将对象直接传递给$.post
(或任何$.ajax方法),jQuery将对其进行内部编码
var data ={foo:'bar'};
$.post('taskmanager.php', data, function(resp){
console.log(resp);
})
php
如果只想使用json,则需要设置适当的内容类型
$.ajax({
url:'...',
method:'post',
contentType:'application/json',
dataType:'json',
data: JSON.stringify(data)
}).done(function(resp){
console.log(resp);
});
然后在php中使用:
$data = json_decode(file_get_contents('php://input'));
$data['foo'] ='Some new value';
echo json_encode($data);
您所做的与
$.post()
@Ibu相同,它的工作方式相同,只是$.ajax在需要时提供了一些额外的灵活性。发送时需要设置contentTypejson@charlietfl不一定。如果没有php,它仍然可以工作。。。将有一个空的$\u POST,因为没有发送键/值对,并且内容类型为form Encoded。您所做的与$相同。POST()
@Ibu,其工作方式相同,除了$。如果需要,ajax提供了一些额外的灵活性。发送时需要设置contentTypejson@charlietfl不一定。如果没有php,它仍然可以工作。。。将有一个空的$u POST,因为没有发送键/值对,并且内容类型是form encodedis easy,但这不是json,这很重要distinction@tamak当前位置你发布的内容看起来很有趣。处理$.post调用的php脚本看起来像什么?很简单,但这不是json,很重要distinction@tamak当前位置你发布的内容看起来很有趣。php脚本如何处理$.post调用?需要先运行服务器才能在页面中使用ajax,然后才能运行php@charlietfl:谢谢。我现在将代码复制到我的WAMP/www区域,我看到它正在尝试运行.php文件。使用本文的其他部分答案,我现在可以运行一些东西了。首先需要运行服务器才能在页面中使用ajax,然后才能运行php@charlietfl:谢谢。我现在将代码复制到我的WAMP/www区域,我看到它正在尝试运行.php文件。使用这篇文章的其他部分答案,我现在可以运行一些东西了。谢谢。我最终没有使用您的ajax示例,但我确实使用了您的php示例代码。我无法告诉您ajax()js示例是否有效,因为我还没有对其进行测试。我最终使用@Shivam发布的.ajax调用。谢谢。我最终没有使用您的ajax示例,但我确实使用了您的php示例代码。我无法告诉您ajax()js示例是否有效,因为我还没有对其进行测试。我最终使用@Shivam发布的.ajax调用。