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 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序
  • ¥15 onvif+openssl,vs2022编译openssl64