Javascript 跨域请求被阻止:同源策略不允许读取 http:// 上的远程资源........

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38663164/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 21:42:19  来源:igfitidea点击:

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://........

javascript

提问by Nahush Sarje

I am trying to communicate with an enterprise application from an web application mainly via javascript using ajax. I tried a lot to solve this issue but not succeeded. I saw several online httppost tool there I am able to see the response text but it is not happening from my end. Each time I am receiving an message like "Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://url. (Reason: CORS header 'Access-Control-Allow-Origin' missing)."

我正在尝试使用ajax主要通过javascript从Web应用程序与企业应用程序进行通信。我尝试了很多来解决这个问题,但没有成功。我在那里看到了几个在线 httppost 工具,我能够看到响应文本,但从我的角度来看并没有发生。每次我收到类似“跨源请求被阻止:同源策略不允许读取http://url 上的远程资源。(原因:CORS 标头“Access-Control-Allow-Origin”丢失)之类的消息时。

My code:

我的代码:

var url = "use_url";
var method = "POST";

var regid = "null";
var UNAME = "abcd089";
var PASSWORD = "abcd007*";
var forLogin = "10 112 " +UNAME+ " " + PASSWORD + " " + regid + " 01";
var async = true;

var request = new XMLHttpRequest();

request.open('POST', url, async);
request.onload = function(){
            //HTTP response
            //if(request.readyState === 4 && request.status === 200){
            var status = request.status;
            var statusData = request.responseText;              

            console.log(status);
            console.log(statusData);
            console.log(request);

        //}
};

request.setRequestHeader("X-Requested-With", "XMLHttpRequest");
request.setRequestHeader("Content-Type", "text/plain;charset=UTF-8");
request.setRequestHeader("Cache-Control", "no-cache");

request.send(forLogin);

I am looking for a solution to get response text. I saw online some solutions but they all are talking about setting response header, but some online httppost sites are working fine on data and producing response text. I am looking solution in javascript.

我正在寻找获取响应文本的解决方案。我在网上看到了一些解决方案,但他们都在谈论设置响应头,但一些在线 httppost 站点在数据和生成响应文本方面工作正常。我正在寻找 javascript 中的解决方案。

采纳答案by jayesh

That is not allowed from javascript side if you are on different domain then you need to do at from server side.

如果您在不同的域中,则 javascript 端不允许这样做,那么您需要从服务器端执行此操作。

browser has cross-origin blocked you can not do any request to non host domain from javascript using ajax. if you are on http://XXXX.comthen you can not call http://YYYY.comfrom javascript for post request

浏览器已被跨域阻止,您无法使用 ajax 从 javascript 向非主机域发出任何请求。如果您在http://XXXX.com 上,则无法从 javascript调用http://YYYY.com以进行发布请求

if you have full control over both domain that you can change your server config to allowed that domain to access resource but that is not preferable as security..

如果您可以完全控制这两个域,则可以更改服务器配置以允许该域访问资源,但这不是安全性可取的。

below code you can use to do http post request from server

以下代码可用于从服务器执行 http post 请求

URL url = new URL("your url");
  HttpURLConnection httpCon = (HttpURLConnection) url.openConnection();
  httpCon.setDoOutput(true);
  httpCon.setRequestMethod("POST");
  OutputStreamWriter out = new OutputStreamWriter(
      httpCon.getOutputStream());
  System.out.println(httpCon.getResponseCode());
  System.out.println(httpCon.getResponseMessage());
  out.close();