62,025
社区成员
发帖
与我相关
我的任务
分享
<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>
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();
crossDomain: true,
xhrFields: {withCredentials: true},
。。。。
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.
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>
我是这样做的,不行。
<!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>