viezel / napp.alloy.adapter.restapi

RestAPI Sync Adapter for Titanium Alloy Framework
197 stars 102 forks source link

READ ERROR on fetch with data #56

Open dottodot opened 9 years ago

dottodot commented 9 years ago

Whenever I do a fetch with data I get an error of

[ERROR] :  [REST API] READ ERROR:
[ERROR] :  {
[ERROR] :      code = 0;
[ERROR] :      data = "Unable to parse JSON string";
[ERROR] :      error = "cannot parse response";
[ERROR] :      responseJSON = "<null>";
[ERROR] :      responseText = "cannot parse response";
[ERROR] :      status = error;
[ERROR] :      success = 0;
[ERROR] :  }
[ERROR] :  [REST API] apiCall ERROR: cannot parse response
[ERROR] :  [REST API] apiCall ERROR CODE: 0
[ERROR] :  [REST API] apiCall ERROR MSG: cannot parse response

here's how I'm fetching

events.fetch({
        data : {
            user : {
                email : Titanium.App.Properties.getString("email"),
                token : Titanium.App.Properties.getString("token")
            }
        },
        processData: true,
        success : function(_model, _response) {
            $.loading.hide();
        },
        error : function(_model, _response) {
            Ti.API.info('Events Fetch Error');
            Ti.API.info(_response);
        }
    });

I've also tried JSON.stringify on the data but that causes the same error.

leitom commented 9 years ago

I found the problem to be the in the apiCall function on ios8 emulator. The problem is when passing anything into xhr.send() function when not needed creates this error.

The following solution worked:

if (_options.type != 'GET' && ! _.empty(_options.data)) {
    xhr.send(_options.data);
} else {
    xhr.send();
}
leitom commented 9 years ago

I someone can verify this I will create a pull request.

viezel commented 9 years ago

that looks correct.. please PR that.