简体   繁体   English

angularjs $ http.get在移动浏览器上不起作用

[英]angularjs $http.get not working on mobile browsers

I tried to use this code: 我尝试使用此代码:

var url = "http://www.test.com?param=1&callback=JSON_CALLBACK"; 

$http.get(url).success(function(data, status, headers, config) {

if (data.success == 1) {

    //do somethings

} else {

    //notice

}

});

It is working fine with browsers of laptop but on mobile browsers, it is not working. 它在笔记本电脑的浏览器上运行良好,但在移动浏览器上却无法运行。

in .htaccess, i added: 在.htaccess中,我添加了:

Header add Access-Control-Allow-Origin "*"

Header add Access-Control-Allow-Headers "x-requested-with, content-type"

Header add Access-Control-Allow-Methods "PUT, GET, POST, DELETE, OPTIONS"

Please help! 请帮忙!

The mobile browser and desktop browser works same way for http calls. 移动浏览器和台式机浏览器的HTTP调用方式相同。 You can make an error block in your code to see what error is coming from the server. 您可以在代码中进行错误阻止,以查看来自服务器的错误。 Below piece of code is what I am suggesting. 下面的代码是我的建议。

this._getAll = function (model) {
      var deferred = $q.defer();
      $http
        .get("your-secure-rest-url")
        .success(function (data) {
          deferred.resolve(data);
        })
        .error(function (reason) {
          deferred.reject(reason);
        });
      return deferred.promise;
 };

As you are allowing Access origin "*" for all domain, there is no problem in client side but you need to check the rest server if it is accessable from all domain or is it restricted to particular domain. 当您允许所有域的访问源“ *”时,客户端没有问题,但是您需要检查其余服务器是否可以从所有域访问,或者是否仅限于特定域。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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