跨域问题,而张贴在第三方网站在angular js

Cross domain issue while posting in third party site in angular js

本文关键字:第三方 网站 angular js 张贴 问题      更新时间:2023-09-26
    HTML Code:                                                                       
    <div  ng-app="myApp" ng-controller="myController">
    <div>
       <input type="button" data-ng-click="submit()"                                                                   value="Continue"></input>
      </div>
    </div>

点击继续按钮后张贴第三方网站不改变URL。在IE浏览器中,它工作得很好,但其他浏览器不工作,我面临以下问题…

Angular Code:   
 var myAppex = angular.module("myApp", []);
 myAppex.controller('myController', ['$scope','$window', '$http','$location',
                   function($scope, $window, $http,$location) {
                $scope.submit = function() {                                   
              var headersSend = {
                                                                                                                 'Access-Control-Allow-Origin': '*',                                                           'Access-Control-Allow-Credentials': 'true',                                                     'Access-Control-Allow-Headers': 'X-Custom-Header,x-requested-with, Content-Type, origin, authorization, accept, client-security-token,Overwrite, Destination, User-Agent, Translate, Range, Content-Range, Timeout, X-File-Size, X-Requested-With, If-Modified-Since, X-File-Name, Cache-Control, Location, Lock-Token, If',                               'Access-Control-Allow-Methods' : 'POST, GET, OPTIONS, PUT',                                                   'Content-Type': 'application/json;charset=utf-8',                                                            'Accept': 'application/json',                                                                };                                                                              $http({method: 'POST',                                                               url:respData.thirdpartyurl,  //ex: https://en.wikipedia.org/wiki/W3Schools                       crossDomain: true, processData: true,                                                                 dataType: 'jsonp',                                                                headers : headersSend,                                                              data: respData.somedata                                                           }).success(function(data,status, headers, config) {                                            $('body').html(data);                                                                         headers  = headersSend;                                                           }).error(function(data, status, headers, config) {                      if(status == 400) {                                                $scope.messages = data; } else {                                         alert('Unexpected server error.');                                   }
                                                                      });
                                                                  };
                                }]);

浏览器控制台中出现问题

XMLHttpRequest无法加载https://en.wikipedia.org/wiki/W3Schools。请求的资源上没有'Access-Control-Allow-Origin'标头。因此,不允许访问源'http://localhost:8081'。响应的HTTP状态码为501..

非常感谢任何帮助。提前感谢

use corsproxy npm install corsproxy .

然后做一个corsproxy。从1337/9292开始…根据它开始的位置,(让我们说9292)附加你的外部url,即在你的情况下" https://en.wikipedia.org/wiki/W3Schools"与http://localhost:9292/en.wikipedia.org/wiki/W3Schools,它会工作良好。