Cjxcz Odjcayrwl
Cjxcz Odjcayrwl

Reputation: 22847

Dojo XHR don't send a request to other server at all, but everything works in jQuery

I have the development enviroment consisting of the Apache HTTP server for fast Javascript development and the application server (WebSphere) that provides the JSON REST API. Of course, Access-Control-Allow-Origin is set (to *).

The following code results in error:

 xhr.post({
  url: "http://localhost:8080/rest-sample/rest/test/list",
  handleAs: "json",
  load: onload
 });

 RequestError: Unable to load
 http://localhost:8080/rest-sample/rest/test/list status: 0
 ErrorCtor()create.js (Zeile 13) onError()xhr.js (Zeile 80)     

 var err = Error.call(this, message),

There is a JavaScript error thrown instead of sending the AJAX request. However, in the same time, the following jQuery snipplet function perfect:

    var url = "http://localhost:8080/rest-sample/rest/test/list"
    $.post(url, {}, onLoad, 'json')

My question is: what I'm doing wrong? How to send the AJAX request to the other server using Dojo?

I'm using dojo 1.9

Upvotes: 0

Views: 4605

Answers (2)

C Snover
C Snover

Reputation: 18766

Your server must also send Access-Control-Allow-Headers: x-requested-with.

Upvotes: 1

Marius
Marius

Reputation: 423

I think xhr.post is no longer supported, i suggest to use dojo/request, or at least dojo/request/xhr

require(["dojo/request/xhr"], function(xhr){
  xhr("http://localhost/rest-sample/rest/test/list", {
    handleAs: "json",
    method: "POST"
  }).then(function(data){
    // Do something with the handled data
  }, function(err){
    // Handle the error condition
  }, function(evt){
    // Handle a progress event from the request if the
    // browser supports XHR2
  });
});

If it's Cross origin problem i would suggest using ReverseProxy on your http server.

add this to your httpd.conf

ProxyPass /rest-sample/ http://localhost:8080/rest-sample/

Upvotes: 1

Related Questions