简体   繁体   中英

angular http - request returns 200 OK, but an error event is fired instead of success

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:

在此处输入图片说明

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);
          });

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 .

you cant mix Javascript and PhP variables

var deferred = $q .defer();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM