如何检查 PHP 在 jQuery post 方法中返回的数据类型
how to check the type of data returned from PHP in jQuery post method
数据类型
[{"id":"1","value":"Google"},{"id":"2","value":"Samsung"}]
现在我有了一个通用函数,可以根据 returned 的数据类型执行某些操作。
对于这个特定的数据,我正在检查它是否是数组。
为了检查 return 的数据类型,我使用了以下自定义函数。
function typeOf (obj) {
return {}.toString.call(obj).split(' ')[1].slice(0, -1).toLowerCase();
}
现在如果我调用 typeOf(1)
它 returns "number" 作为答案
如果我调用 typeOf("foo")
它 return "string" 作为答案
问题
调用 typeOf(data)
其中 data = [{"id":"1","value":"Google"},{"id":"2","value":"Samsung"}]
从 php 页面回显它给出 "string" 作为答案
如果接收到的数组类型数据变量通过以下函数作为
传递,它只是returns数组
data = jQuery.parseJSON(data);
现在,我了解到 php 的回显可能是字符串形式,但没有任何方法可以像我的 php 页面那样更改它正在关注
echo json_encode(array(array(some_key=>some_data)));
无论您从 PHP 或 JSP 调用 AJAX 得到什么值,都会 return 将其作为字符串。您需要解析字符串以使脚本正确理解数据类型。你可以这样做:
data = JSON.parse(data);
if (typeof data == "object") {
// JSON Data
} else {
// String Data
}
片段
* {font-family: 'Segoe UI';}
<script>
function parseData(data) {
if (data.length > 0)
data = JSON.parse(data);
if (typeof data == "object")
return "This is JSON with keys.";
else
return "This is a String of " + data.length + " characters.";
}
</script>
<p><strong>JSON Object</strong></p>
<p><script>document.write(parseData('{"a": "Alphabet", "b": "Fun"}'));</script></p>
<p><strong>String / Number</strong></p>
<p><script>document.write(parseData(''));</script></p>
我假设您是通过 ajax 获取该数据的。您是否在 ajax 函数中指定了 dataType: 'json'
?如果不指定dataType,jQuery会猜测,有时会猜错。
(编辑: 更多关于 $.ajax
函数的信息:http://api.jquery.com/jquery.ajax/ )
要让 jQuery 知道您正在发送 JSON 数据,服务器必须使用正确的 Content-Type
header 进行响应。在 PHP 中,这可以通过在任何输出之前调用 header('Content-Type: application/json');
轻松实现,并且 jQuery 将尝试解析响应。
这将导致成功处理程序的 data
参数成为包含 JSON 值的 object。确保检查您是否获得了 object
类型的数据变量,因为任何其他类型都应该表明格式错误的 JSON 数据来自您的服务器。
$.ajax({
url: '/endpoint.php',
method: "POST",
data: {key:'value'},
success: function(data){
if (typeof data !== 'object')
return console.error('Invalid data', data);
// Do something with the response
}
});
数据类型
[{"id":"1","value":"Google"},{"id":"2","value":"Samsung"}]
现在我有了一个通用函数,可以根据 returned 的数据类型执行某些操作。
对于这个特定的数据,我正在检查它是否是数组。 为了检查 return 的数据类型,我使用了以下自定义函数。
function typeOf (obj) {
return {}.toString.call(obj).split(' ')[1].slice(0, -1).toLowerCase();
}
现在如果我调用 typeOf(1)
它 returns "number" 作为答案
如果我调用 typeOf("foo")
它 return "string" 作为答案
问题
调用 typeOf(data)
其中 data = [{"id":"1","value":"Google"},{"id":"2","value":"Samsung"}]
从 php 页面回显它给出 "string" 作为答案
如果接收到的数组类型数据变量通过以下函数作为
传递,它只是returns数组data = jQuery.parseJSON(data);
现在,我了解到 php 的回显可能是字符串形式,但没有任何方法可以像我的 php 页面那样更改它正在关注
echo json_encode(array(array(some_key=>some_data)));
无论您从 PHP 或 JSP 调用 AJAX 得到什么值,都会 return 将其作为字符串。您需要解析字符串以使脚本正确理解数据类型。你可以这样做:
data = JSON.parse(data);
if (typeof data == "object") {
// JSON Data
} else {
// String Data
}
片段
* {font-family: 'Segoe UI';}
<script>
function parseData(data) {
if (data.length > 0)
data = JSON.parse(data);
if (typeof data == "object")
return "This is JSON with keys.";
else
return "This is a String of " + data.length + " characters.";
}
</script>
<p><strong>JSON Object</strong></p>
<p><script>document.write(parseData('{"a": "Alphabet", "b": "Fun"}'));</script></p>
<p><strong>String / Number</strong></p>
<p><script>document.write(parseData(''));</script></p>
我假设您是通过 ajax 获取该数据的。您是否在 ajax 函数中指定了 dataType: 'json'
?如果不指定dataType,jQuery会猜测,有时会猜错。
(编辑: 更多关于 $.ajax
函数的信息:http://api.jquery.com/jquery.ajax/ )
要让 jQuery 知道您正在发送 JSON 数据,服务器必须使用正确的 Content-Type
header 进行响应。在 PHP 中,这可以通过在任何输出之前调用 header('Content-Type: application/json');
轻松实现,并且 jQuery 将尝试解析响应。
这将导致成功处理程序的 data
参数成为包含 JSON 值的 object。确保检查您是否获得了 object
类型的数据变量,因为任何其他类型都应该表明格式错误的 JSON 数据来自您的服务器。
$.ajax({
url: '/endpoint.php',
method: "POST",
data: {key:'value'},
success: function(data){
if (typeof data !== 'object')
return console.error('Invalid data', data);
// Do something with the response
}
});