Success callback not called

Jul 4, 2013 at 9:05 PM
I'm calling a service using the following code:

var oHeaders = {
              'Accept' : 'text/html,application/xhtml+xml,application/xml,application/json',
              "dataserviceversion": "3.0",
              "maxdataserviceversion": "3.0"
              };

      oHeaders['X-SUP-URLREWRITE-ENABLED'] = "True";

      var request =
      {
        headers : oHeaders,
        requestUri : "http://services.odata.org/OData/OData.svc/Categories",
        data: null,
      };
      OData.read(request, successFunction, failFunction, handlerFunction);
The response status I get is 200 but it doesn't call successFunction, it calls failFunction. If not the response status, what determines which callback is called?

Ross
Coordinator
Jul 10, 2013 at 10:16 PM
Hi rhightower

Thanks for posting the question.

I have a few questions to be able to answer this.
what browser are you using ?
what does the parameter handlerFunction do ? did you implement your own response handler ?
what is the value of the parameter passed to the fail function, does it specify a specific error ?

Hussein
Jul 11, 2013 at 2:38 AM
Thanks for the reply Hussein,

This is my code:

...
      var oHeaders = {
              "Accept" : "text/html,application/xhtml+xml,application/xml,application/json",
              "dataserviceversion": "3.0",
              "maxdataserviceversion": "3.0"
              };

      oHeaders['X-SUP-URLREWRITE-ENABLED'] = "True";

      var request =
      {
        headers : oHeaders,
        requestUri : "http://smp.ucc.uwm.edu:8000/TestService",
        data: null,
        user: "C103",
        password: "mobile"
      };
      OData.read(request, successFunction, failFunction, handlerFunction);
...


function successFunction(data, request){
alert("Success");      
}

function failFunction(err){
alert("fail");
var foo = JSON.stringify(err); 
alert(foo);
$("#error").html(foo);
$.mobile.listview.prototype.enhanceWithin($.mobile.activePage);
}

function handlerFunction(){
alert("handler");
}


The response includes this:

...
"request":{"headers":{
"Accept":"application/json",
"X-SUP-URLREWRITE-ENABLED":"True",
...
"statusCode":200,
"statusText":"OK"
...
{\"odata.metadata\":\"http://services.odata.org/OData/OData.svc/$metadata
#Products\",\"value\":[{\"ID\":0,\"Name\":\"Bread\",\"Description\":\"Whole grain bread\"
...

In fact, all the expected data is returned. When the app runs I see the alert with fail, the alert with JSON.stringify(err). It's an app on a iOS simulator.

Thanks,

Ross
Coordinator
Jul 11, 2013 at 7:30 PM
You need to pass the handlerFunction as null, it's needed only when you want to create a custom reader for the response. I'm guessing that the failure is due the function read doesn't exist on the handler, can you try that and tell me if it still fails ?
Jul 11, 2013 at 10:37 PM
I tried:

OData.read(request, successFunction, failFunction, null);

and:

OData.read(request, successFunction, failFunction);

and get the same result.

Baffling.

Ross
Coordinator
Jul 11, 2013 at 11:21 PM
In order to debug this you can follow these steps:

1 - Use the non minified version of DataJS.
2 - look in the datajs code for a function called request in odata.defaultHttpClient object.
3 - There is a part that calls the success callback according to this condition if (statusCode >= 200 && statusCode <= 299)
4 - try adding breakpoint ( or alerts if you can't do that on the simulator ) in this place and see why is your success callback is not called and please tell me what you get.

Hope this helps

Hussein
Jul 13, 2013 at 2:30 PM
It appears that the problem is that the call is successful, the data is returned and the status is 200, but datajs cannot identify a handler for the data. I tried adding a handler callback to the oData.read:

OData.read(request, success, error, customHandler );

However, customHandler is not called.

Ross
Jul 13, 2013 at 2:38 PM
I got it. I looked at the data returned in a browser and noticed that the type was application/atom+xml. Once I added that type it worked. Trivial but this is new to me.

I really appreciate your help, it made all the difference.

Ross