0

I make an https request to outer service in http://transltr.org/.

var reqUrl = "http://transltr.org/api/translate?text=dog&to=he";

      var deferred = $q.defer();
          $http.get(reqUrl).then(function(response) {
                    deferred.resolve(response);
                   alert("sada");
         },function (error) {
                             deferred.reject(error);
                         });


          return deferred.promise;

rrsponse is 200, and I get data:

enter image description here

but error alert fired. There is a way to get this data?

I also see error in console :

XMLHttpRequest cannot load http://transltr.org/api/translate?text=dog&to=he. The 'Access-Control-Allow-Origin' header contains multiple values '*, *', but only one is allowed. Origin 'http://myDomain.co.il' is therefore not allowed access.

I also tried this version(in this case, no response at all):

   var reqUrl = "http://transltr.org/api/translate";
 var params = {
  to: to,
  text: text
  };

      var deferred = $q.defer();
        var request = {
                      method: 'GET',
                      url: reqUrl,
                      dataType: "json",
                      contentType: "application/json; charset=utf-8",
                      headers: {
                          'Content-Type': 'application/json',
                          'Cache-Control': 'no-cache',
                          'X-Requested-With': 'XMLHttpRequest',
                          'Access-Control-Allow-Origin': '*'
                      },
                      params: params
                  };


          $http(request).success(function (data) {
              deferred.resolve(data);
          }).error(function (error) {
              deferred.reject(error);
          });
georgeawg
  • 48,608
  • 13
  • 72
  • 95
  • what backend you are using? I think it is web api use this [**answer**](http://stackoverflow.com/questions/40574068/delete-verb-working-in-postman-but-not-with-ajax/40574368#40574368) – Aravind Apr 13 '17 at 07:01
  • From the error message, it may be related to CORS. Check this one, it might help http://stackoverflow.com/questions/37594403/the-access-control-allow-origin-header-contains-multiple-values – CrazyMac Apr 13 '17 at 07:06
  • Try removing "dataType", "contentType" and "headers" from your above code.. Well, this might be seen as a CORS issue. – Rohan Kangale Apr 13 '17 at 07:09
  • I tried to send a request on jsbin.com, with the same result (but omitting the extra headers and type information). So the server obviously sends an illegal CORS header in its response (which is not present for a simple browser request, I checked). So either you can contact the API provider or you won't be able to do anything about it (other than using your own server with a proxy script) – devnull69 Apr 13 '17 at 07:12
  • Possible duplicate of [The 'Access-Control-Allow-Origin' header contains multiple values](http://stackoverflow.com/questions/37594403/the-access-control-allow-origin-header-contains-multiple-values) – georgeawg Apr 13 '17 at 07:13
  • 1
    `Access-Control-Allow-Origin` is not a request header.... it is a response header that server has to set – charlietfl Apr 13 '17 at 07:35
  • any way to solve it with contact the API and without server side? – user2837319 May 03 '17 at 10:24

2 Answers2

0

If you're using Chrome on Windows, do the following to disable the CORS filter.

Exit chrome. Press Windows Icon then letter R as you keep windows key down. Paste the command below on the resulting box and press enter.

chrome.exe --user-data-dir="C:/Chrome dev session" --disable-web-security

This will open Chrome with websecurity disabled(no CORS filter). Now reload your app on this Chrome window and let me know of any resulting exceptions. This is to aid you achieve what you want to, but you have to handle CORS in your API.

Felix Too
  • 11,614
  • 5
  • 24
  • 25
-7

you cant mix Javascript and PhP variables

var deferred = $q.defer();