簡體   English   中英

IISNOde中的請求資源(AngularJS + NodeJs)上沒有“ Access-Control-Allow-Origin”標頭

[英]No 'Access-Control-Allow-Origin' header is present on the requested resource (AngularJS + NodeJs) In IISNOde

嗨,我有一個Angular Aplication,它從NodeJS中的REST API使用,都部署在IIS服務器中,而我正在使用IISNode。

我收到錯誤XMLHttpRequest cannot load http://181.49.53.186:8006/DynamicContent/form/list. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://web.procesosyservicios.info:3006' is therefore not allowed access. XMLHttpRequest cannot load http://181.49.53.186:8006/DynamicContent/form/list. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://web.procesosyservicios.info:3006' is therefore not allowed access. 僅在生產版本中,在開發人員版本中,永遠不會收到錯誤。 奇怪的是,例如始終無法獲得錯誤,例如,使用先前的方法,每10次調用我只會得到2 o 3次錯誤。

我的app.js文件具有以下配置:

router.use(function(req, res, next) {
  res.header('Access-Control-Allow-Origin', '*');
  res.header('Access-Control-Allow-Methods', 'GET, POST');
  res.header('Access-Control-Allow-Headers': 'Origin, X-Requested-With, Content-Type, Authorization');
  next();
});

或者,我刪除前面的行,並在我的web.coonfig文件中定義相同的行,如下所示:

<httpProtocol>
    <customHeaders>
        <add name="Access-Control-Allow-Origin"  value="*" />
        <add name="Access-Control-Allow-Methods" value="GET,POST" />
        <add name="Access-Control-Allow-Headers" value="X-Requested-With,content-type,Authorization" />
    </customHeaders>
</httpProtocol>

但是結果是一樣的,有時只能工作。 我不知道可能是什么錯誤。

謝謝您的幫助

就我而言,解決方案是按照此配置並添加以下行來更改我的web.config文件:

<httpProtocol>
    <customHeaders>
        <add name="Access-Control-Allow-Origin"  value="*" />
        <add name="Access-Control-Allow-Methods" value="GET,POST" />
        <add name="Access-Control-Allow-Headers" value="X-Requested-With,content-type,Authorization" />
    </customHeaders>
</httpProtocol>

跨域Ajax請求的問題對於開發人員來說是相當普遍的。

解決此問題的最佳方法是從服務器端向響應對象添加標頭。

這是一個確實詳細解釋了這一點的博客。 它還包括一些服務器端代碼。 在c#中,添加標頭的方法是通過以下語句。

Response.AddHeader("Access-Control-Allow-Origin", "mydomain.com");
Response.ContentType = "application/json";

您也可以添加*代替mydomain.com,但它違反了阻止此類請求的安全措施的全部目的。

希望能有所幫助。

謝謝

帕拉斯

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM