13

I have run into a problem with the AJAX response using jquery (and coldfusion serverside). Sometimes it works and sometimes it doesn't. When I test the serverside component calling it directly - it always works, so I guess the problem is with the ajax response. I get the serverurl, directly from the server - so it shouldn't be a cross-domain issue. I just show and hide div-tags so it shouldn't be about relocating the url before the ajax response has been processed. I am pulling out a lot of hair over this.

The code for the addTask method:

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, 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);
        }
    });
}

Firebug output shows that the AJAX calls works sometimes and sometimes it fails.:

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, status=0, statusText="error"}

UPDATE:

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

UPDATE:

Using fiddler I get this error:

HTTP Error 411. The request must be chunked or have a content length.

Any help is much appreciated.

Thanks,

Peter

rrk
  • 15,677
  • 4
  • 29
  • 45
Dechen
  • 161
  • 1
  • 5
  • What does Firebug show as the request and response content for the failed requests? Does the server process the requests at all? Are tasks added when the addTask method is invoked? Could it be a problem with content-type negotiation? If your request expects one kind of response and the server is sending something else that could cause errors. I'd look very carefully at the request/response from a direct call vs one run by your code. I use fiddler for this (it supports Diff-ing pairs of requests, which is exactly what you want to do here) – barnyr Jan 16 '13 at 10:09
  • I have updated the question with the request-header. I don't get a response back only the ErrorMsg from the returned error: Object { readyState=0, status=0, statusText="error"}. I have visited the server logs which shows nothing (Railo coldfusion engine). The task is not added. – Dechen Jan 16 '13 at 10:47
  • When I call the server method directly from browser, it returns the id on the inserted task i.e. "131" - as it should. – Dechen Jan 16 '13 at 10:53
  • @barnyr I have changed the ajax call to accept all kinds of response. Now the request-header says: Accept */*. So I guess the response is not looking for a specific result. No change, still same error – Dechen Jan 16 '13 at 12:06
  • Well, now it's narrowed down to a problem with the request rather than the response. The error you're seeing seems to be complaining that there's no content-length header, although I can see one in the example request headers you posted. Try capturing both good and failing requests in Fiddler, then save them both out (right click, save, request, entire request) and compare them (use the Beyond Compare trial if you don't already have diff software). There will be a difference causing this. – barnyr Jan 16 '13 at 13:00
  • readyState=0 means that the request wasn't even sent out (readyStatus goes from 0 to 4: 0=not initialized, 1=server connection established, etc). And status would usually contain the HTTP status (ex: 200, 300, 400, 500 range responses), having the status=0 means that there's no status to return because the request hasn't been sent out (makes sense). And you don't see any request in your server logs. You must find why the request ONLY SOMETIMES isn't sent out. For ex: could it be something else interupting your ajax request or cancelling it? clicking on some other button in rapid succession? – Pierre Sep 04 '19 at 21:20

1 Answers1

-6

I think you need quotes in the data parameter like this:

data: { 'userid': settings.userid, 
        'taskname': $('#ff-add-task-name').val(),
        'tasknote': $('#ff-add-task-note').val(),
        'completed': $('#ff-add-task-completed').val(),
        'priority': priority,
      },

See if that helps.

MrRaymondLee
  • 546
  • 5
  • 12
  • 3
    The quotes are irrelevant in this case. This is a JS object literal, not a JSON strings. JSON string, yes, they need proper quoting on every field name. – sergiopereira Sep 12 '13 at 19:22