C#转Javascript提示'Access-Control-Allow-Origin' header is present on the requested
<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();
...全文
相关推荐
解决方案 ‘Access-Control-Allow-Origin’ header in the response must not be the wildcard ‘*’
以下的问题由我斌哥提供解决方案 https://me.csdn.net/Duktig19 他的 个人站点. 文中内容由本人测试和整理 环境 文中使用的环境是 "name": "vue-admin-template", "version": "3.8.0" using npm@6.4.1 using node@v10.14.2 先看三张图,正常都是服务器返回session,然后浏览器每次发请求都会带上这个sessino 原创文章 24获赞 1访问量 1200
Access-Control-Allow-Origin跨域问题的终极解决,给自己做备份
Access-Control-Allow-Origin跨域问题的终极解决,给自己做备份,内含允许访问特定域名或多个域名
异常 AJAX 跨域请求报:No ‘Access-Control-Allow-Origin‘ header is present
测试代码
快速解决Canvas.toDataURL 图片跨域的问题
出现Canvas.toDataURL 图片跨域问题怎么解决呢?下面小编就为大家带来一篇Canvas.toDataURL 图片跨域问题的快速解决方法。一起跟随小编过来看看吧
NODE.JS跨域问题的完美解决方案
主要介绍了NODE.JS跨域问题的完美解决方案,非常不错具有参考借鉴价值,需要的朋友可以参考下


