jQuery 如何从ajax请求中获取数据响应?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19901837/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 00:46:57  来源:igfitidea点击:

How to get the data response from ajax request?

javascriptjqueryajaxcodeigniter

提问by Jerielle

I just want to ask how can I preview the data from my ajax request? I just want to know if the data is correct.

我只是想问如何从我的ajax请求中预览数据?我只想知道数据是否正确。

Here's my code.

这是我的代码。

<script type="text/javascript">

            var globalBase_Url = "{$base_url}" + "index.php/user_controller/processAdd/";   

            //alert(globalBase_Url);

            {literal}

                $(document).ready(function(){

                    $('#add_cat').on('click',function(){
                        $('#add_category').show('slide');
                    });

                    $('#submit').on('click',function(){

                        var name = $('#category_name').val();
                        var desc = $('#description').val();

                        console.log(globalBase_Url);

                        $.ajax({
                            type: 'POST',
                            url: globalBAse_Url,
                            data: {cat_name:name,cat_desc:desc}, //How can I preview this?
                            dataType: 'json',
                            async: false,
                            success: function(d){

                            }
                        });

                    });

                });

            {/literal}


        </script>

In my controller I have this.

在我的控制器中,我有这个。

public function processAdd(){

        $cat_name = $this->input->post('cat_name');
        $cat_desc = $this->input->post('cat_desc');



 }

I used the chrome developer tools and preview it's response in XHR. But I don't see my data. By the way I am using CodeIgniter

我使用了 chrome 开发人员工具并在 XHR 中预览它的响应。但是我看不到我的数据。顺便说一句,我正在使用 CodeIgniter

回答by Drixson Ose?a

in your controller:

在您的控制器中:

you should have something like this

你应该有这样的东西

public function processAdd(){


  if($this->input->post()){

   //do something
   $var1 = $this->input->post('cat_name');
   $var2 = $this->input->post('cat_desc');

   echo json_encode(array('status' => 'ok')); //must be encode with array to access it in your response as an object since you use DataType:json
  }

}

then in your ajax, to access the response

然后在你的 ajax 中,访问响应

 $.ajax({
  type: 'POST',
  url: '/controllers/processAdd',
  data: {cat_name:name,cat_desc:desc}, //How can I preview this?
  dataType: 'json',
  async: false, //This is deprecated in the latest version of jquery must use now callbacks
  success: function(d){
   alert(d.status); //will alert ok
  }
});

回答by Arun P Johny

You can use the browser developer tools > NetWork

您可以使用浏览器开发者工具 > 网络

Chrome: Developer Tools
FireFox: Developer Tools- Network/Fire Bug-> Network

Chrome:开发者工具
FireFox:开发者工具-网络/ Fire Bug->网络

or

或者

in the ajax success handler, log the data to the console

在 ajax 成功处理程序中,将数据记录到控制台

success: function(d){
    console.log(d);// then look at the developer tools -> console
}

回答by abhiklpm

try putting

尝试放置

    var globalBase_Url = "{$base_url}" + "index.php/user_controller/processAdd/"; 

inside {literal} tag

{literal} 标签内