te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>javascript - How to debug ajax request error? - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to debug ajax request error? - Stack Overflow

programmeradmin2浏览0评论

I'm trying to get a JSON, hosted in a local server here, but for some reason it doesn't work. If i hit my url on the browser it returns the Json correctly and If I use another URL, I do get some data.

My original getJson method:

getJson:

$.getJSON(url, 

                    function(data) {
                        console.log('got something'); 
                        alert(data);    

                    });

Now I want to see exactly what's wrong. I've rewritten it as a Ajax request, and on my error callback jqxhr gives me a lot of things that I don't understand. Can i get more details about this error ?

TextStatus value is "error"

errorThrown is null

    $.ajax({
                  url: url,
                  dataType: 'json',
                  success:  function(data) {
                        console.log('got something'); 
                        alert(data);    
                        },
                error: function(jqxhr,textStatus,errorThrown)
                    {
                        console.log(jqxhr);
                            console.log(textStatus);
                            console.log(errorThrown);                               

                            for (key in jqxhr)
                                alert(key + ":" + jqxhr[key])                                                                 
                            for (key2 in textStatus)
                                alert(key + ":" + textStatus[key])
                            for (key3 in errorThrown)
                                alert(key + ":" + errorThrown[key])

                   //<--- All those logs/alerts, don't say anything helpful, how can I understand what error is going on? ---->

                }});

Last, the json i'm supposed to get back is

[{"message": "login failed"}]

And I can't use firebug, chrome console, or any other dev tools because this is a mobile app, developed using Phonegap!

I'm trying to get a JSON, hosted in a local server here, but for some reason it doesn't work. If i hit my url on the browser it returns the Json correctly and If I use another URL, I do get some data.

My original getJson method:

getJson:

$.getJSON(url, 

                    function(data) {
                        console.log('got something'); 
                        alert(data);    

                    });

Now I want to see exactly what's wrong. I've rewritten it as a Ajax request, and on my error callback jqxhr gives me a lot of things that I don't understand. Can i get more details about this error ?

TextStatus value is "error"

errorThrown is null

    $.ajax({
                  url: url,
                  dataType: 'json',
                  success:  function(data) {
                        console.log('got something'); 
                        alert(data);    
                        },
                error: function(jqxhr,textStatus,errorThrown)
                    {
                        console.log(jqxhr);
                            console.log(textStatus);
                            console.log(errorThrown);                               

                            for (key in jqxhr)
                                alert(key + ":" + jqxhr[key])                                                                 
                            for (key2 in textStatus)
                                alert(key + ":" + textStatus[key])
                            for (key3 in errorThrown)
                                alert(key + ":" + errorThrown[key])

                   //<--- All those logs/alerts, don't say anything helpful, how can I understand what error is going on? ---->

                }});

Last, the json i'm supposed to get back is

[{"message": "login failed"}]

And I can't use firebug, chrome console, or any other dev tools because this is a mobile app, developed using Phonegap!

Share Improve this question edited Dec 6, 2012 at 12:14 caiocpricci2 asked Dec 6, 2012 at 11:43 caiocpricci2caiocpricci2 7,79810 gold badges57 silver badges88 bronze badges 8
  • Is the URL on the same domain, subdomain and protocol as your current page? – devnull69 Commented Dec 6, 2012 at 11:46
  • use firebug or chrome's developer tool and by clicking on the network and xhr you can see the response from the url – Manigandan Arjunan Commented Dec 6, 2012 at 11:52
  • Did you check with Firebug/Fiddler/Chrome dev tool's Network tab to see what was the response code and text? – Murali Murugesan Commented Dec 6, 2012 at 11:53
  • The URL is in a different domain. I can't check with firebug because I'm running this on a phone it's a Phonegap app. I probably should have said that in the question! – caiocpricci2 Commented Dec 6, 2012 at 11:54
  • 1 Seems its returning something other than valid JSON? Try accepting a different datatype and see what content is ing back? – Kodemon Commented Dec 6, 2012 at 12:10
 |  Show 3 more ments

2 Answers 2

Reset to default 7

I am gonna put my ments into answer form, its not a solution to your problem but its a good way to troubleshoot various return issues with jQuery Ajax.

When you are requesting a certain datatype in this case dataType: 'json' you are requesting a specific type of data. If that specific dataType is not returned it will pass to the error function.

If your error message isnt providing information that will explain your problem then try requesting a dataType that is more friendly. This way you can see what your request is actually returning. If its data other than your requested dataType then that is your issue and you can work it out from there.

dataType like html or text will be very open to give you the exact data returned from the requested url and you can solve your issues from there.

Hope it helps a little.

jqxhr - XrayWrapper
textStatus - represents the text representation of Https response status (eg. success, error)
errorThrown - XMLHttpRequest

jqxhr.status - gives the Http response code such as 200, 404, 500 so on..

Use this code snippet to print all the key in the json object

var output = '';
for (property in object) {
  output += property + ': ' + object[property]+'; ';
}
console.log(output);
发布评论

评论列表(0)

  1. 暂无评论