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

LiXingYi1986 LiXingYi1986 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区

<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();
...全文

相关推荐

评论 18
收藏 已收藏
分享
返回顶部
LiXingYi1986
.NET社区 62,272 成员 668,968 内容