PHP-如何设置JSON响应来自文件的哪个打印信息?
因此,当我发送json时,它会等待resonse,而发送json的文件中发生的一切都会被视为响应。如何设置哪种回显/打印信息,因为除了回显/确切地说是响应之外,还会有其他内容?例如,我有这段代码,我想做一些其他的事情,而不是在这种情况下: PHP: jQuery:PHP-如何设置JSON响应来自文件的哪个打印信息?,php,jquery,json,response,Php,Jquery,Json,Response,因此,当我发送json时,它会等待resonse,而发送json的文件中发生的一切都会被视为响应。如何设置哪种回显/打印信息,因为除了回显/确切地说是响应之外,还会有其他内容?例如,我有这段代码,我想做一些其他的事情,而不是在这种情况下: PHP: jQuery: $.ajax({ url: "./index.php", type: "POST", data: { example:"exampl
$.ajax({
url: "./index.php",
type: "POST",
data: {
example:"example",
example2:$(".content").text()
},
dataType: "json"
}).done(function(data){
$('#response').append('<p style="color:red;">'+data+'</p>');
}).fail(function(){
$('#response').append('<p style="color:red;">Error</p>');
});
$.ajax({
url:“./index.php”,
类型:“POST”,
数据:{
示例:“示例”,
示例2:$(“.content”).text()
},
数据类型:“json”
}).完成(功能(数据){
$(“#响应”).append(“”+数据+”
”);
}).fail(函数(){
$('#response')。追加('Error
');
});
默认情况下,您将获得HTTP 200 Ok响应
我不知道您在某些情况下试图发送一个错误,因此您的ajax请求会通过失败函数
您可以通过以下方法在php中实现这一点:
header($_SERVER['SERVER_PROTOCOL'] . ' 500 Internal Server Error', true, 500);
问候我不明白你有什么问题 但是您有一个bug,可能是因为您无法在javascript中获得json对象
if($_POST){
$return=array("Success.");
echo json_encode($return);
exit;
}
json_encode需要一个数组:)对不起,您很难理解。你的母语是什么?德语?(请用德语写下pastebin并发送给我) 除了返回成功字符串,您还可以执行其他任何操作。 例如,将每个值写入文件,例如 在开始时构建一个数组,并在结束时返回它。 然后,您可以在脚本运行时添加一些内容 我希望这有帮助:) index.php
<?php
if($_POST)
{
$response = array();
foreach($_POST as $file => $content)
{
$response[] = 'Writed ' . file_put_contents($file, $content) . ' bytes to file ' . $file;
}
if(!count($response))
{
header('HTTP/1.0 400 Bad Request', true, 400);
}
echo json_encode($response);
exit;
}
?><html>
<head>
<title>25760456</title>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript">
$(function()
{
$.ajax({
url: "./index.php",
type: "POST",
data: {
example:"example",
example2:$(".content").text()
},
dataType: "json"
}).done(function(data){
$('#response').append('<p style="color:red;">'+data+'</p>');
}).fail(function(){
$('#response').append('<p style="color:red;">Error</p>');
});
});
</script>
<style type="text/css">
#response {
border: 1px solid #000;
padding: 5px;
}
</style>
</head>
<body>
<input type="text" class="content" />
<div id="response"></div>
</body>
</html>
25760456
$(函数()
{
$.ajax({
url:“./index.php”,
类型:“POST”,
数据:{
示例:“示例”,
示例2:$(“.content”).text()
},
数据类型:“json”
}).完成(功能(数据){
$(“#响应”).append(“”+数据+”
”);
}).fail(函数(){
$('#response')。追加('Error
');
});
});
#回应{
边框:1px实心#000;
填充物:5px;
}
看起来像
您到底想实现什么?您想访问
示例:
和示例2:
?是的,我想访问示例和示例2的值并将其保存(可能是并修改)到文件/db或其他文件中。这不应该被添加到响应中,因为它会失败。不,我不想这样做。我在问题中添加了我想做的事情。我编辑了问题。如果你现在不明白,我无法更简单/更清楚地解释它。
<?php
if($_POST)
{
$response = array();
foreach($_POST as $file => $content)
{
$response[] = 'Writed ' . file_put_contents($file, $content) . ' bytes to file ' . $file;
}
if(!count($response))
{
header('HTTP/1.0 400 Bad Request', true, 400);
}
echo json_encode($response);
exit;
}
?><html>
<head>
<title>25760456</title>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript">
$(function()
{
$.ajax({
url: "./index.php",
type: "POST",
data: {
example:"example",
example2:$(".content").text()
},
dataType: "json"
}).done(function(data){
$('#response').append('<p style="color:red;">'+data+'</p>');
}).fail(function(){
$('#response').append('<p style="color:red;">Error</p>');
});
});
</script>
<style type="text/css">
#response {
border: 1px solid #000;
padding: 5px;
}
</style>
</head>
<body>
<input type="text" class="content" />
<div id="response"></div>
</body>
</html>