C#转Javascript提示'Access-Control-Allow-Origin' header is present on the requested

LiXingYi1986 2020-04-03 07:00:28

<script type="text/javascript">
$(function () {
var settings = {
"async": true,
"crossDomain": true,
"url": "http://192.168.1.60/ISAPI/System/Video/inputs/channels/1/overlays/text",
"method": "PUT",
"headers": {
"Access-Control-Allow-Origin": "*",
"Authorization": "Basic YWRtaW46enp4eTEyMzQ1Ng==",
"cache-control": "no-cache",
"Postman-Token": "0c288f09-09c6-4088-b79b-c969b2833124"
},
"data": " <TextOverlayList size=\"500\"> <TextOverlay> <id>1</id> <enabled>true</enabled> <positionX>10</positionX> <positionY>70</positionY> <displayText>东风4-1868 11/小时 DK200+509</displayText> </TextOverlay> </TextOverlayList>"
}

$.ajax(settings).done(function (response) {
layer.msg("<span style='color:#ffffff;'>己更新!</span>");
});
});
</script>


执行上段代码提示:(上面代码是下面的C#转换,通过POSTman执行是没问题的,但放在浏览器就不行了,客户端 又不能安装IIS服务器编译aspx文件。)怎么解决?
http://192.168.1.60/ISAPI/System/Video/inputs/channels/1/overlays/text 401 (Unathorized)
'Access-Control-Allow-Origin' header is present on the requested resource.


下面的C#执行没问题,我需要在客户端JS脚本运行成功,因为是局域网IP。


string URI = "http://192.168.1.60/ISAPI/System/Video/inputs/channels/1/overlays/text";
Stream outstream = null;
Stream instream = null;
StreamReader sr = null;

//新版定义HTTP的请求,不能用new,https://msdn.microsoft.com/zh-cn/library/system.net.httpwebresponse(v=vs.110).aspx
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(URI);
// 要注意的这是这个编码方式,还有内容的Xml内容的编码方式
Encoding encoding = Encoding.GetEncoding("UTF-8");
byte[] data = System.Text.Encoding.UTF8.GetBytes(xmlPath);
req.Method = "PUT";
req.ContentType = "text/plain;charset=UTF-8";
//添加用户名密码完成Authorization认证
string username = "admin";
string password = "zzxy123456";
string userPasswd = username + ":" + password;
CredentialCache mycache = new CredentialCache();
mycache.Add(new Uri(URI), "Basic", new NetworkCredential(username, password));
req.Credentials = mycache;
req.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(new ASCIIEncoding().GetBytes(userPasswd)));
//发送内容
outstream = req.GetRequestStream();
outstream.Write(data, 0, data.Length);
outstream.Flush();
outstream.Close();

HttpWebResponse res = (HttpWebResponse)req.GetResponse();
//接收NVR返回数据信息
res = req.GetResponse() as HttpWebResponse;
//直到req.GetResponse()程序才开始向目标网页发送Post请求
instream = res.GetResponseStream();
sr = new StreamReader(instream, encoding);
//返回结果网页代码
string content = sr.ReadToEnd();
Console.Write(content);
Console.Read();
...全文
1165 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
yuhong66 2020-04-09
  • 打赏
  • 举报
回复
iis要设置允许跨域访问才行
LiXingYi1986 2020-04-09
  • 打赏
  • 举报
回复
引用 17 楼 正怒月神 的回复:
加了 Access-Control-Allow-Origin 后,又报了什么错呢?
还是一样的错误, http://192.168.1.60/ISAPI/System/Video/inputs/channels/1/overlays/text 401 (Unauthorized) Access to XMLHttpRequest at 'http://192.168.1.60/ISAPI/System/Video/inputs/channels/1/overlays/text' from origin 'http://192.168.1.112' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
正怒月神 版主 2020-04-09
  • 打赏
  • 举报
回复
加了 Access-Control-Allow-Origin 后,又报了什么错呢?
LiXingYi1986 2020-04-09
  • 打赏
  • 举报
回复
我把具体应用场景说一下:一台在郊外移动的汽车,车上安装了海康摄像头和安装有安卓操作系统的工控机,工控机里有我写的JS程序,用http://192.168.1.60/ISAPI/System/Video/inputs/channels/1/overlays/text 可以写数据到摄像头上,也就是说只能在局域网访问才能成功。这个摄像头的API是无法进入修改,也没有IIS服务器。 我的c#代码是用我的笔记本电脑直接连接摄像头可以访问成功,换成JS就跨域失败,我想只能用过JS在客户端访问局域网摄像头的API才可以成功。
xiaoxiangqing 2020-04-08
  • 打赏
  • 举报
回复
iis要设置允许跨域访问才行
正怒月神 版主 2020-04-07
  • 打赏
  • 举报
回复
需要两段

crossDomain: true,                 
xhrFields: {withCredentials: true}, 
。。。。
LiXingYi1986 2020-04-07
  • 打赏
  • 举报
回复
IIS加了也不起作用
楠小南 2020-04-07
  • 打赏
  • 举报
回复
引用 10 楼 LiXingYi1986 的回复:
[quote=引用 8 楼 楠小南 的回复:]
[quote=引用 6 楼 楠小南 的回复:]
跨域了,出现 Access-Control-Allow-Origin 表示 服务器没有对跨域做处理,浏览器拒绝了你的响应
跨域要在 服务器 Response 设置 头部 "Access-Control-Allow-Headers": "*" 放行,浏览器端正常请求即可


被楼上带偏了,应该加 Access-Control-Allow-Origin[/quote]

问题是 服务器 http://192.168.1.60 就硬件的地址,无法在它的网页里加 Access-Control-Allow-Origin[/quote]

不是在网页里加,是在你服务器的响应头里面加 或者在iis 里面加
LiXingYi1986 2020-04-07
  • 打赏
  • 举报
回复
引用 8 楼 楠小南 的回复:
[quote=引用 6 楼 楠小南 的回复:] 跨域了,出现 Access-Control-Allow-Origin 表示 服务器没有对跨域做处理,浏览器拒绝了你的响应 跨域要在 服务器 Response 设置 头部 "Access-Control-Allow-Headers": "*" 放行,浏览器端正常请求即可
被楼上带偏了,应该加 Access-Control-Allow-Origin[/quote] 问题是 服务器 http://192.168.1.60 就硬件的地址,无法在它的网页里加 Access-Control-Allow-Origin
LiXingYi1986 2020-04-07
  • 打赏
  • 举报
回复
错误提示:

jquery-1.10.2.min.js:23 OPTIONS http://192.168.1.60/ISAPI/System/Video/inputs/channels/1/overlays/text 401 (Unauthorized)
 Access to XMLHttpRequest at 'http://192.168.1.60/ISAPI/System/Video/inputs/channels/1/overlays/text' from origin 'http://192.168.1.112' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
楠小南 2020-04-07
  • 打赏
  • 举报
回复
引用 6 楼 楠小南 的回复:
跨域了,出现 Access-Control-Allow-Origin 表示 服务器没有对跨域做处理,浏览器拒绝了你的响应
跨域要在 服务器 Response 设置 头部 "Access-Control-Allow-Headers": "*" 放行,浏览器端正常请求即可


被楼上带偏了,应该加 Access-Control-Allow-Origin
LiXingYi1986 2020-04-07
  • 打赏
  • 举报
回复
引用 5 楼 正怒月神 的回复:
[quote=引用 4 楼 LiXingYi1986 的回复:] [quote=引用 3 楼 正怒月神 的回复:] 需要两段

crossDomain: true,                 
xhrFields: {withCredentials: true}, 
。。。。
还是不行[/quote] 在headers在添加一个试试看 "Access-Control-Allow-Headers": "*",[/quote]

           var settings = {
                async: true,
                crossDomain: true,
                xhrFields: { withCredentials: true },
                url: "http://192.168.1.60/ISAPI/System/Video/inputs/channels/1/overlays/text",
                method: "PUT",
                headers: {
                    "Access-Control-Allow-Origin": "*",
                    "Access-Control-Allow-Methods": "POST,GET,PUT",
                    "Access-Control-Allow-Headers': 'x-requested-with,content-type",
                    "Authorization": "Basic YWRtaW46enp4eTEyMzQ1Ng==",
                    "cache-control": "no-cache",
                    "Postman-Token": "0c288f09-09c6-4088-b79b-c969b2833124"
                },
                "data": "  <TextOverlayList size=\"500\">    <TextOverlay>      <id>1</id>      <enabled>true</enabled>      <positionX>10</positionX>      <positionY>70</positionY>      <displayText>东风4-1868  11/小时  DK200+509</displayText>    </TextOverlay>  </TextOverlayList>"
            }


            $.ajax(settings).done(function (response) {
                layer.msg("<span style='color:#ffffff;'>己执行!</span>");
            });
html的head标签也加了 <head> <meta http-equiv="Access-Control-Allow-Origin" content="*" /> </head> 我是这样做的,不行。
楠小南 2020-04-07
  • 打赏
  • 举报
回复
跨域了,出现 Access-Control-Allow-Origin 表示 服务器没有对跨域做处理,浏览器拒绝了你的响应
跨域要在 服务器 Response 设置 头部 "Access-Control-Allow-Headers": "*" 放行,浏览器端正常请求即可
正怒月神 版主 2020-04-07
  • 打赏
  • 举报
回复
引用 4 楼 LiXingYi1986 的回复:
[quote=引用 3 楼 正怒月神 的回复:] 需要两段

crossDomain: true,                 
xhrFields: {withCredentials: true}, 
。。。。
还是不行[/quote] 在headers在添加一个试试看 "Access-Control-Allow-Headers": "*",
LiXingYi1986 2020-04-07
  • 打赏
  • 举报
回复
引用 3 楼 正怒月神 的回复:
需要两段

crossDomain: true,                 
xhrFields: {withCredentials: true}, 
。。。。
还是不行
LiXingYi1986 2020-04-04
  • 打赏
  • 举报
回复
是这样加在html头部吗?head之间 <meta http-equiv="Access-Control-Allow-Origin" content="*" />

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Access-Control-Allow-Origin" content="*" />
    <title>************</title>
    <script src="/TrainDB/Scripts/jquery-1.10.2.min.js"></script>
    <script src="/TrainDB/Scripts/layer/layer.js"></script>
    <style>
    </style>
</head>
<body>
    <form id="aspnetForm" runat="server">
    <script type="text/javascript">
        $(function () {
            var settings = {
                "async": true,
                "crossDomain": true,
                "url": "http://192.168.1.60/ISAPI/System/Video/inputs/channels/1/overlays/text",
                "method": "PUT",
                //"dataType": "jsonp",
                "headers": {
                    "Access-Control-Allow-Origin": "*",
                    "Access-Control-Allow-Methods": "POST,GET,PUT",
                    "Authorization": "Basic YWRtaW46enp4eTEyMzQ1Ng==",
                    "cache-control": "no-cache",
                    "Postman-Token": "0c288f09-09c6-4088-b79b-c969b2833124"
                },
                "data": "  <TextOverlayList size=\"500\">    <TextOverlay>      <id>1</id>      <enabled>true</enabled>      <positionX>10</positionX>      <positionY>70</positionY>      <displayText>东风4-1868  11/小时  DK200+509</displayText>    </TextOverlay>  </TextOverlayList>"
            }


            $.ajax(settings).done(function (response) {
                layer.msg("<span style='color:#ffffff;'>己更新!</span>");
            });
           
    </script>
    </form>
</body>
</html>

引用 1 楼 wanghui0380 的回复:
"crossDomain": true, 这个没有对应翻译,毕竟她声明了是跨域访问。你目前返回的也是“'Access-Control-Allow-Origin”跨域访问失败 所以你再提交时候,请再头部区域加入:Access-Control-Allow-Origin标记
wanghui0380 2020-04-03
  • 打赏
  • 举报
回复
"crossDomain": true, 这个没有对应翻译,毕竟她声明了是跨域访问。你目前返回的也是“'Access-Control-Allow-Origin”跨域访问失败 所以你再提交时候,请再头部区域加入:Access-Control-Allow-Origin标记

62,025

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧