douxi1968 2017-03-05 10:41
浏览 168

AJAX:如何从php接收布尔值

I have a questions and I was searching on StackOverFlow, but for some reasons the examples I found didn't work correctly.

I have a following Ajax script (I just sending 2 variables to php file to check if they are in an array):

    $.ajax({ url: 'info.php',
         data: {'row':row, 'col':col},
         type: 'GET',
        dataType: 'json',
         success:  function(){
              console.log(row + col);
});

But the thing is that I don't know how to properly send (PHP) data back to "Ajax" and how to capture it:

I have been trying with:

   echo json_encode($Result);  //Where $Result is a boolean

The data is sent correctly from ajax to .php file, I recieve a status 200 and if I go to .php file I can also see that the data was properly recieved.

I was using information from here: How to get true or false from PHP function using AJAX?

And here: ajax sucess: i can not check if the returned information is true or false

I have tried implementations from both cases. I would appreciate any tips.

  • 写回答

1条回答 默认 最新

  • drxnfdx798517235 2017-03-05 11:20
    关注

    There are a few things I would check in your situation - first is, as you've already checked, making sure the response you're getting is valid. As it's 200, we can rule that out.

    The second thing I'd check is that your data is coming back in the proper json format, because when you specify "datatype: 'json'" you're telling the ajax call to "treat this as a json object", meaning if it's malformed you'll actually receive an error.

    Try adding an error function to your ajax object:

    $.ajax({
        url: 'info.php',
        data: {'row': row, 'col': col},
        type: 'GET',
        dataType: 'json',
        success:  function() {
            console.log(row + col);
        },
        error: function(jqXHR, textStatus, errorThrown) {
            console.log(errorThrown);
        }
    });
    

    That will at least tell you whether or not the output is coming back correctly.

    If you still don't get output after that, it's possible that console.log is being tied up by a library you're using, so maybe try an alert() instead. Other than that we might need more information, so tell me what happens once the error function is in place.

    评论

报告相同问题?

悬赏问题

  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 路易威登官网 里边的参数逆向
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程
  • ¥20 模型在y分布之外的数据上预测能力不好如何解决
  • ¥15 processing提取音乐节奏
  • ¥15 gg加速器加速游戏时,提示不是x86架构
  • ¥15 python按要求编写程序
  • ¥15 Python输入字符串转化为列表排序具体见图,严格按照输入