如何添加访问控制 - 允许 - 原产地? [英] How to add Access-Control-Allow-Origin?

查看:108
本文介绍了如何添加访问控制 - 允许 - 原产地?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到以下错误,当我打电话给我的web服务的方法。

 产地的http://本地主机:4165不是由访问控制 - 允许 - 产地允许的。
 

当谈到网我越来越喜欢将解决方案添加访问控制 - 允许 - 原产地 我不知道在哪里可以添加此。 我的脚本是:

  $(文件)。就绪(函数(){
        $阿贾克斯({
            类型:邮报,数据类型:JSON,则contentType:应用/ JSON的;字符集= UTF-8,
            网址:HTTP://本地主机:63384 / ListWebService.asmx / HelloWorld的,成功的:功能(数据){警报(data.d); }错误:函数(请求,状态,错误){
                警报(request.responseText);
            }
        });
    });
 

和我的web服务的方法是:

  [WebMethod的]
    公共字符串的HelloWorld()
    {
        返回你好用户;
    }
 

解决方案

我已经找到了我的问题的答案。就在下面添加到您的web.config文件

 < system.webServer>
    < httpProtocol>
        < customHeaders>
            <添加名称=访问控制 - 允许 - 起源值=*/>
            <添加名称=访问控制 - 允许 - 头值=原产地,X-要求,通过,内容类型,接受/>
        < / customHeaders>
    < / httpProtocol>
< /system.webServer>
 

I am getting the following error when I call my webservice method.

Origin http://localhost:4165 is not allowed by Access-Control-Allow-Origin.

When referring net I am getting solution like add Access-Control-Allow-Origin I dont know where to add this. My script is :

 $(document).ready(function () {
        $.ajax({
            type: "Post", dataType: "json", contentType: "application/json; charset=utf-8",
            url: "http://localhost:63384/ListWebService.asmx/HelloWorld", success: function (data) { alert(data.d); }, error: function (request, status, error) {
                alert(request.responseText);
            }
        });
    });

And my webservice method is :

[WebMethod]
    public string HelloWorld()
    {
        return "Hello User";
    }

解决方案

I have found the answer for my question. Just add the following to your web.config file

<system.webServer>
    <httpProtocol>
        <customHeaders>
            <add name="Access-Control-Allow-Origin" value="*"/>
            <add name="Access-Control-Allow-Headers" value="Origin, X-Requested-With, Content-Type, Accept" />
        </customHeaders>
    </httpProtocol>
</system.webServer>

这篇关于如何添加访问控制 - 允许 - 原产地?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆