在Javascript中进行POST时阻止了跨域请求

时间:2019-12-03 09:38:15

标签: javascript ajax cors xmlhttprequest

我尝试在javascript中发出POST请求,以使用API​​使用以下代码将永久令牌转换为永久令牌:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/" xmlns:arr="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
   <soapenv:Header/>
   <soapenv:Body>
      <tem:GetMigrationData201901>
         <!--Optional:-->
         <tem:AuthToken>*MY AUTH TOKEN HERE*</tem:AuthToken>
         <!--Optional:-->
         <tem:NIP>
            <!--Zero or more repetitions:-->
            <arr:string>5532317349</arr:string>
         </tem:NIP>
      </tem:GetMigrationData201901>
   </soapenv:Body>
</soapenv:Envelope>

执行此操作时,在Firefox的控制台中出现以下错误:

 var location = window.location.search;
 var params = location.search('code=');
 var code = location.substring(params+5, location.length);
 var clientID = XXXX;
 var clientSecret = "YYYYYYYYYY";
 var http = new XMLHttpRequest();
 var ParamConnex = "client_id="+clientID+"&client_secret="+clientSecret+"&code="+code; 
 var url = "https://test-sandbox/2.0/auth/token/access/";
 http.onreadystatechange = function (response) {
        console.log(this.readyState, this.status);
        if(response.target.readyState === 4 && response.target.status === 200) {
          console.log(http.responseText);
        }
      };
 http.open("POST", url, true);
 http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
 http.send(ParamConnex);

经过研究,我发现我可以使用Access-Control-Allow-Origin来解决此问题:*在htaccess中,但事实是我无法访问服务器。 您知道我该如何解决该错误吗?

0 个答案:

没有答案