Angular JS $http request does not reach the server in ie8

老子叫甜甜 提交于 2019-12-12 06:45:55

问题


I'm having issues with using $http on ie8. The request does not reach the server, until I hit a refresh. Coming back to the same link still has the same problem until I hit refresh again.

The weird thing is if the web server is on LAN and the request is made to a server in LAN, it works fine. But if the webserver is hosted remotely, it does not work!

Here is the code:

Index.html

{{test}}

Controller

app.controller(
    "TestController",
    function( $scope, $http) {
        var url = '/test/get_data';
        $http.get(url).success(function(data) {
            $scope.test = data;
        });
    }
);

I got this error: TypeError: Object doesn't support this property or methodundefined

I prepared a JSFiddle earlier but JSFiddle is broken in ie8 so I don't provide it here.

Unfortunately I don't have a remote server that I can share with you.

Edit Previously I used an external url which gave me 'Access Denied' error in ie because of Same Origin Policy as mentioned by one answer below. But this was not my original problem. I still have the issue above when request is from the same origin


回答1:


This is a cross domain request, which is not allowed in ajax because of Same Origin Policy.

There are two solutions for this
1. JSONP: It is a cross browser way to handle cross domain ajax requests using javascript callback mechanism
2. CORS: It is a HTML5 standard, it is implemented by most of the modern browsers except IE

Mongodb lab is not supporting jsonp since it has support for CORS, that is why your request is failing in IE and works in Chrome and other browsers.

As per this post they do not have any plan to support jsonp, so I don't thick there is a way to make this work in IE.




回答2:


So I found the fix... Hope this helps anyone out there that experience this problem

  1. Angular script needs to be loaded after jQuery. I didn't have this because Yii framework that I use autoloads jQuery and the angular was not included after the jQuery.
  2. All the controller functions need to be at the end of body section (just before the closing )
  3. Updating to angular 1.0.5 seems to fix the problem. The problem occurred in 1.0.4 with all the above tricks. I think is related to fix 791804bd


来源:https://stackoverflow.com/questions/15330549/angular-js-http-request-does-not-reach-the-server-in-ie8

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!