Error with $batch request while sending multiple queries

Oct 4, 2012 at 12:10 PM

I am getting an error which says "d.request is not a function".
Please help me with this error.
Also while debugging the error shows up at line where I have included the datajs library.

Code.......................................................................................................................................

OData.defaultHttpClient.enableJsonpCallback = true;

 
              OData.request( {
                  headers: { Accept: "application/json" },
                    requestUri: "http://odata.netflix.com/v2/Catalog/$batch",
                    method: "POST",
                    data: { __batchRequests: [
                       { requestUri: "Genres?$filter=Name eq 'Adventures'", method: "GET" }
                   
                    ]}
                }, function (data, response) {
                       batchResponses = data.__batchResponses;
                       var i, len;
                       for(i = 0, len = batchResponses.length; i < len; i++){
                          data = batchResponses[i].data;
                           // do something interesting with this individual response data.
                       }
                       document.getElementById("target-element-id").innerHTML = data;
                    },  function(err){
                    alert(err.message);
                },undefined, OData.batchHandler);


.......................................................................................................................................              

 

Thank you.

Oct 4, 2012 at 7:07 PM

Hi Anriuddhaverma,

    The snippet above is passing the OData.batchHandler into the wrong argument of the request funciton.  It should be:

OData.defaultHttpClient.enableJsonpCallback = true;
OData.request( {
        headers: { Accept: "application/json" },
        requestUri: "http://odata.netflix.com/v2/Catalog/$batch",
        method: "POST",
        data: { __batchRequests: [
            { requestUri: "Genres?$filter=Name eq 'Adventures'", method: "GET" }
        ]}
    }, function (data, response) {
            batchResponses = data.__batchResponses;
            var i, len;
            for(i = 0, len = batchResponses.length; i < len; i++) {
                data = batchResponses[i].data;
                // do something interesting with this individual response data.
            }
            document.getElementById("target-element-id").innerHTML = data;
    },  function(err){
            alert(err.message);
    }, OData.batchHandler);

 Now, keep in mind that batch requests have to be done via POST and this verb is not supported by JSONP... with JSONP you can only do GET requests (even batch ones).

Regards,

Alex Trigo.

Oct 8, 2012 at 6:28 AM

Thank you Alex for to timely reply.

I have another conceptual doubt... If batch requests can be made only using POST,
and POST is not supported by JSONP.
How can one send batch request with cross browser referencing??
because if I remove

"OData.defaultHttpClient.enableJsonpCallback = true;"

from the above code, I get an error saying,
"HTTP request failed"

Thank you,
Aniruddha Verma
Oct 16, 2012 at 8:08 PM

Hi Anriuddhaverma,

You can fallback to other techniques to send cross domain POST requests.  One way is to use CORS, but unfortunately the standard is just staring to see support around the board and not all the browsers and servers support it yet.  The other solution is to use a reverse proxy... a dummy endpoint in the same domain of the web appplication that relays the request to the target endpoint in the different domain. So far, this last option is what would give you true cross browser support.  JSONP is really a solution for just bringing data into the browser.. not sending it back.

Regards,

Alex Trigo.