我使用
jquery(和coldfusion serverside)遇到了AJAX响应问题.有时候它有效,有时却没有.当我测试直接调用它的服务器端组件时 – 它总是有效,所以我猜问题是ajax响应.我直接从服务器获取serverurl – 因此它不应该是跨域问题.我只是显示和隐藏div-tags所以它不应该是在处理ajax响应之前重新定位url.我在这上面拔了很多头发.
function addTask(){ var priority = $('#ff-add-task-priority').length > 0? $('#ff-add-task-priority').val() : 0; $.ajax({ url: settings.server+'/c/Tasks.cfc?method=addTask',data: { userid: settings.userid,taskname: $('#ff-add-task-name').val(),tasknote: $('#ff-add-task-note').val(),completed: $('#ff-add-task-completed').val(),priority: priority,},type: 'POST',dataType: 'json',success: function(response) { var output = addRow(response,$('#ff-add-task-name').val(),0); $('#data-list-tasks').append(output); $('#main').children().addClass('hide'); $('#section-list-tasks').removeClass('hide'); resetForm($('#add-task-form')); //route('#section-list-tasks'); },error: function(ErrorMsg) { console.log('Error',ErrorMsg); } }); }
POST http://dev.wedoolist.com/c/Tasks.cfc?method=addTask jquery.min.js (linje 2) Error Object { readyState=0,status=0,statusText="error"} #secti...t-tasks (linje 124) POST http://dev.wedoolist.com/c/Tasks.cfc?method=getTasks 200 OK 152ms jquery.min.js (linje 2) POST http://dev.wedoolist.com/c/Tasks.cfc?method=addTask 200 OK 146ms jquery.min.js (linje 2) POST http://dev.wedoolist.com/c/Tasks.cfc?method=addTask 200 OK 133ms jquery.min.js (linje 2) POST http://dev.wedoolist.com/c/Tasks.cfc?method=addTask 200 OK 133ms jquery.min.js (linje 2) POST http://dev.wedoolist.com/c/Tasks.cfc?method=addTask 200 OK 131ms jquery.min.js (linje 2) POST http://dev.wedoolist.com/c/Tasks.cfc?method=addTask jquery.min.js (linje 2) Error Object { readyState=0,statusText="error"}
更新:
Request-headere Accept application/json,text/javascript,*/*; q=0.01 Accept-Encoding gzip,deflate Accept-Language da,en-us;q=0.7,en;q=0.3 Content-Length 59 Content-Type application/x-www-form-urlencoded; charset=UTF-8 Cookie CFID=198de696-2168-4911-8639-79ea944c9975; CFTOKEN=0; JSESSIONID=B520084E7DDFB504BC87E200449C3DA7 Host dev.wedoolist.com Referer http://dev.wedoolist.com/index.cfm?add-task-completed-switch=0&ff-add-task-priority=0 User-Agent Mozilla/5.0 (Windows NT 5.1; rv:18.0) Gecko/20100101 Firefox/18.0 X-Requested-With XMLHttpRequest
更新:
使用fiddler我收到此错误:
HTTP Error 411. The request must be chunked or have a content length.
任何帮助深表感谢.
谢谢,
彼得
解决方法
我认为你需要数据参数中的引号,如下所示:
data: { 'userid': settings.userid,'taskname': $('#ff-add-task-name').val(),'tasknote': $('#ff-add-task-note').val(),'completed': $('#ff-add-task-completed').val(),'priority': priority,
看看是否有帮助.