duandange7480 2015-02-13 07:32
浏览 19
已采纳

什么在jQuery AJAX中响应?

I'm sorry to add this broad question here, but I cannot seem to google it myself, I tried, and I really can't grasp the idea behind this, so here goes.
So I have a web-site that uses AJAX to login a user without full page reload. I kind of understand how AJAX works, and I'm sorry for my ignorance, but in my case:

$.ajax({
            url: './',
            type: 'POST',
            data: {auth: auth, login: login, pass: pass}
            success: function(res){
               // Code that checks if **res** is a specific string
            },
            error: function(){
                alert("Error!");
            }

I understand that this sends POST request to the same page with 3 parameters. What I don't understand is what specifically does it get as a responce? In my case, in res is the $_SESSION element that contains the string message.
My question is: how do I know what gets in the responce? If I would just echo something in my function, would that get in the responce? Is there like a documentation about what can be passed to the arguments of success function?
I'm really confused about this.

  • 写回答

4条回答 默认 最新

  • dongyong9224 2015-02-13 07:41
    关注

    The "res"... or commonly "data" in most examples, is simply the reply data from your page that your posting to..

    So say in the case of PHP... you yes would simply echo anything back to it.

    commonly people use JSON, so with php you would create a array with all the data you want to send back and then simply do

    YOUR PAGE THAT SENDS THE POST

    <script>
        // JUST USING SUCCESS HERE ATM (Tthis does not show the full ajax command)
        // Refer to original question for full javascript
        success: function(res){
          var myData = $.parseJSON(res);
          if(myData.hasOwnProperty('name')){
              alert(myData.name);
          }
          if(myData.hasOwnProperty('object1') && myData.object1.hasOwnProperty('items')){
              alert(myData.object1.items.one);
          }
        },
    </script>
    

    YOUR PHP PAGE THAT RESPONDS

    <?php
      $myResponse = array();
      $myResponse['name'] = "John Doe";
      $myResponse['number'] = 123456789;
      $myResponse['other'] = "and so on";
      $myResponse['object1'] = array();
      $myResponse['object2'] = array();
    
      $myResponse['object1']['name'] = "john";
      $myResponse['object1']['items'] = array();
      $myResponse['object1']['items']['one'] = "one one 1";
      $myResponse['object1']['items']['two'] = "two two 2";
    
      $myResponse['object2']['name'] = "jane";
    
    
      echo json_encode($myResponse);
    ?>
    

    By using a "multidimensional" array in php, you can then treat each part of the array as a separate section/object

    This might help: http://www.thecave.info/pass-a-php-array-to-javascript-as-json-using-ajax-and-json_encode/

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作