I'm pretty new to Jquery Ajax and sorry if that's a simple thing.
我是Jquery Ajax的新手,对不起,如果那是一件简单的事情。
So my problem here is that I want to retrieve a json object from my php file, but it only works when I echo only one Json object like this :
所以我的问题是我想从我的php文件中检索一个json对象,但只有当我只回显一个这样的Json对象时它才有效:
<?php
header('Content-type: application/json; charset=utf-8');
$arr = array ('Lastname'=>'Doe','Firstname'=>'John','Age'=>109);
echo json_encode($arr);
When I want to echo another json or some text returned as json in my javascript file, I don't get anything back from the ajax call.
当我想在我的javascript文件中回显另一个json或一些以json形式返回的文本时,我从ajax调用中得不到任何回复。
<?php
// This code doesn't work
header('Content-type: application/json; charset=utf-8');
$arr = array ('Lastname'=>'Doe','Firstname'=>'John','Age'=>109);
echo 'hi there';
$json = 'Hello people';
echo json_encode($json);
echo json_encode($arr);
My ajax call is this :
我的ajax电话是这样的:
$.ajax({
url: 'test.php',
type: 'GET',
success: function (response) {
console.log(response);
}
});
So my final question is : what's wrong in my code, and what's not allowed in ajax call
所以我的最后一个问题是:我的代码有什么问题,以及ajax调用中不允许的内容
4 个解决方案
#1
4
So, the output of this code
所以,这段代码的输出
// This code doesn't work
header('Content-type: application/json; charset=utf-8');
$arr = array ('Lastname'=>'Doe','Firstname'=>'John','Age'=>109);
echo 'hi there';
$json = 'Hello people';
echo json_encode($json);
echo json_encode($arr);
Will be:
将会:
hi there"Hello people"{"Lastname":"Doe","Firstname":"John","Age":109}
Which is not valid JSON
哪个是无效的JSON
Correct way of responding with JSON is to use just one call to json_encode
:
使用JSON进行响应的正确方法是只使用一次调用json_encode:
header('Content-type: application/json; charset=utf-8')
$response = [
'message' => 'hi there',
'people' => [
['Lastname'=>'Doe', 'Firstname'=>'John', 'Age'=>109],
],
];
echo json_encode($response);
#2
6
You can only put one JSON object in a response. If you have more than one thing you want to send, just make an array:
您只能在响应中放置一个JSON对象。如果您要发送多个内容,只需创建一个数组:
echo json_encode([
'user' => ['Lastname'=>'Doe', 'Firstname'=>'John', 'Age'=>109],
'other' => 'some other stuff',
'more' => ['even', 'more', 'suff'],
]);
You also don't want any extraneous output before or after the JSON, so remove this:
您也不希望在JSON之前或之后有任何无关的输出,因此请删除此:
echo 'hi there';
#3
1
You cannot merge two JSON encoded objects as this results in invalid JSON syntax.
您无法合并两个JSON编码对象,因为这会导致无效的JSON语法。
What you can do is build a single array/object, encode into JSON and echo the resulting string.
你可以做的是构建一个数组/对象,编码成JSON并回显结果字符串。
The encoded string is the only output you can send back to the client.
编码的字符串是您可以发送回客户端的唯一输出。
#4
0
You cannot send multiple JSON responses via single output. Use Websockets for that or group responses into bag:
您无法通过单个输出发送多个JSON响应。使用Websockets将响应或组响应分组到包中:
json_decode([$json, $arr])
#1
4
So, the output of this code
所以,这段代码的输出
// This code doesn't work
header('Content-type: application/json; charset=utf-8');
$arr = array ('Lastname'=>'Doe','Firstname'=>'John','Age'=>109);
echo 'hi there';
$json = 'Hello people';
echo json_encode($json);
echo json_encode($arr);
Will be:
将会:
hi there"Hello people"{"Lastname":"Doe","Firstname":"John","Age":109}
Which is not valid JSON
哪个是无效的JSON
Correct way of responding with JSON is to use just one call to json_encode
:
使用JSON进行响应的正确方法是只使用一次调用json_encode:
header('Content-type: application/json; charset=utf-8')
$response = [
'message' => 'hi there',
'people' => [
['Lastname'=>'Doe', 'Firstname'=>'John', 'Age'=>109],
],
];
echo json_encode($response);
#2
6
You can only put one JSON object in a response. If you have more than one thing you want to send, just make an array:
您只能在响应中放置一个JSON对象。如果您要发送多个内容,只需创建一个数组:
echo json_encode([
'user' => ['Lastname'=>'Doe', 'Firstname'=>'John', 'Age'=>109],
'other' => 'some other stuff',
'more' => ['even', 'more', 'suff'],
]);
You also don't want any extraneous output before or after the JSON, so remove this:
您也不希望在JSON之前或之后有任何无关的输出,因此请删除此:
echo 'hi there';
#3
1
You cannot merge two JSON encoded objects as this results in invalid JSON syntax.
您无法合并两个JSON编码对象,因为这会导致无效的JSON语法。
What you can do is build a single array/object, encode into JSON and echo the resulting string.
你可以做的是构建一个数组/对象,编码成JSON并回显结果字符串。
The encoded string is the only output you can send back to the client.
编码的字符串是您可以发送回客户端的唯一输出。
#4
0
You cannot send multiple JSON responses via single output. Use Websockets for that or group responses into bag:
您无法通过单个输出发送多个JSON响应。使用Websockets将响应或组响应分组到包中:
json_decode([$json, $arr])