AJAX 请求返回某种形式的长字符串
发生了一些奇怪的事情,我的 AJAX 请求返回一个数组,但整个事情本身就是一个字符串。
这基本上概括了它:
PHP
$item = array();
$item[] = array(
'title' => 'awesome title',
'permalink' => 'some url'
);
json_encode($item);
jQuery
$.ajax({
type: 'post',
url: ajaxurl,
data: {
action: 'a_grid_callback',
type: method
},
success: function(msg) {
console.debug(msg);
}
});
调试返回这样:
[{"title":"awesome title","permalink":"some url"}]0
如果我要执行 alert(msg.length)
我会得到一个很长的值数字相当于上面代码的长度。
Something weird is going on, my AJAX Request is returning a array but the whole thing is a string itself.
This basically sums it up:
PHP
$item = array();
$item[] = array(
'title' => 'awesome title',
'permalink' => 'some url'
);
json_encode($item);
jQuery
$.ajax({
type: 'post',
url: ajaxurl,
data: {
action: 'a_grid_callback',
type: method
},
success: function(msg) {
console.debug(msg);
}
});
The debug returns this:
[{"title":"awesome title","permalink":"some url"}]0
If I was to do alert(msg.length)
I would get a long number equivalent to the length of the code above.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
发布评论
评论(4)
~没有更多了~
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
尝试设置
dataType: 'json'
看起来浏览器正在将响应解释为字符串。Try to set
dataType: 'json'
Looks like the browser is interpreting the response as a string.