json_encode返回200并且未定义 [英] json_encode returns 200 and undefined

查看:66
本文介绍了json_encode返回200并且未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一个代码来添加或删除数据库书签。
代码已准备就绪,它正确地添加和删除数据库书签,但是当我调用该函数时,它会一直返回 json错误而不是 json成功即使代码有效。

I'd like to have a code to add or remove from database bookmarks. The code is ready and it adds and removes from database bookmarks correctly, but when I call the function it keeps returning the json error instead json success even if the code works.

我想知道代码有什么问题(我从其他地方得到并适应) )因为客户端没有收到正确的值 true false ,它只会触发 json beforeSending json错误

I'd like to know what's wrong with the code (that I got and adapt from somewhere else) because the client side is not receiving the correct values true or false, it only triggers the json beforeSending and json error.

服务器端:

if($isFavorite) {
    // if it's favorite, remove from bookmarks
    return json_encode(array("status" => true, "added" => false));
} else {
    // if it's not favorite, include into bookmarks
    return json_encode(array("status" => false, "added" => true));
}

客户端:

<script>
    function addItemToUsersList(userId, type, itemId) {
        jQuery.ajax({
            'url': 'xxx', 
            'type': 'GET',
            'dataType': 'json', 
            'data': {userid: userId, type: type, itemid: itemId}, 
            'success': function(data)  {
                console.log('json success');
            },
            'beforeSend': function() {
                console.log('json beforeSending');
            },
            'error': function(data) {
                console.log('json error');
                console.log(data.status + ' ' + data.added);
            }
        });
    }
</script>

console.log(data.status +''+ data.added ); 行日志 200 undefined

如何返回正确的值<$对于status和<$ c $,c $ c> true 或 false c>已添加?

How may I return the correct values true or false for both "status" and "added"?

编辑:
json success 永远不会登录到控制台,所以我不知道服务器端发生了什么。我需要知道这一点,因为我需要更改一个元素的类来显示一个空的或黄色的星。

The json success is never logged on console, so I don't know what happened on server side. I need to know this because I need to change the class of an element to display an empty or yellow star.

推荐答案

如果你是返回结果并且没有在其他地方做任何回复,你不会得到任何对你的ajax调用的响应,所以它是 undefined 。正如@MikeC所说,你必须在某个时候 echo

If you are returning the result and not doing anything with that return elsewhere, you will not get any response to your ajax call, so it's undefined. As @MikeC says, you must echo it at some point.

如果你还没有在其他地方回应它,尝试:

If you are not already echoing it elsewhere, try:

$response = array(
    'status' => $isFavourite,
    'added' => !$isFavourite
);
echo json_encode($response);

我的建议是,如果'status'和'added'实际上恰好彼此相反时间,那么你可能只需要自己发送'状态'。在你的JS中,如果你想知道添加的值是什么,你可以检查'status'并反转我在上面做的布尔值。

My suggestion is also if 'status' and 'added' are really just the opposite of each other every time, then you probably only need to send 'status' on its own. In your JS you can just check 'status' and reverse the boolean as I've done above, if you want to know what the value of added would be.

 var added = !data.status;






更新

如果你的ajax请求回到错误函数,请求本身可能会失败。

If your ajax request is coming back to the error function, the request itself is probably failing.

将错误功能更改为此,以调试发生的事情:

Change your error function to this, to debug what has happened:

'error': function(jqXHR, status, error) {
    console.log(status);
    console.log(error);
}

您可能在服务器端代码中遇到错误或正在调用错误的PHP脚本也许?

You might have an error in server-side code somewhere or you're calling the wrong PHP script perhaps?

这篇关于json_encode返回200并且未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆