Azure日志分析使用javascript在纯HTML中发布/搜索查询
我想将事件上载到Azure日志分析,并使用带有XMLHTTPRequest或JQuery rest的纯JavaScript检索事件。 1.使用powershell invoke webrequest,我可以上传事件,但不能通过以下html请求上传事件 2. 需要帮助从Azure日志分析重新启动API检索事件 对于第一个示例,这里是示例代码HTTP405:BAD方法-不支持使用的HTTP谓词。Azure日志分析使用javascript在纯HTML中发布/搜索查询,javascript,jquery,powershell,azure,Javascript,Jquery,Powershell,Azure,我想将事件上载到Azure日志分析,并使用带有XMLHTTPRequest或JQuery rest的纯JavaScript检索事件。 1.使用powershell invoke webrequest,我可以上传事件,但不能通过以下html请求上传事件 2. 需要帮助从Azure日志分析重新启动API检索事件 对于第一个示例,这里是示例代码HTTP405:BAD方法-不支持使用的HTTP谓词。 var strBody = { "Hostname": "sdfsfsdf
var strBody = {
"Hostname": "sdfsfsdf",
"Customer": "sdfsfdsfdsf",
"RoundTripLatencyInMs": 67,
};
debugger;
customerId = "xxxxxxxxxxxxxxx";
sharedkey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
logType = "xxxxxxxxx";
TimeStampField = "YYYY-MM-DDThh:mm:ssZ";
// encodebody = stringToUtf16ByteArray(JSON.stringify(strBody).toString('utf8'));//encode_utf8(JSON.stringify(strBody));
str = strBody;
var byteArray = [];
for (var i = 0; i < str.length; i++)
if (str.charCodeAt(i) <= 0x7F)
byteArray.push(str.charCodeAt(i));
else {
var h = encodeURIComponent(str.charAt(i)).substr(1).split('%');
for (var j = 0; j < h.length; j++)
byteArray.push(parseInt(h[j], 16));
}
// return byteArray;
encodebody = byteArray;
method = 'POST';
resource = '/api/logs';
//contentType = 'application/json; charset=utf-8';
var d =new Date();
msdate = d.toUTCString(); //'Thu, 14 Jul 2017 06:35:52 GMT';
contentLength = encodebody.length;
//Signature
xHeaders = "x-ms-date:" + msdate;
stringToHash = method + "\n" + contentLength + "\n" + xHeaders + "\n" + resource;
//message=stringToHash;
var hash = CryptoJS.HmacSHA256(stringToHash, sharedkey);
var hashInBase64 = CryptoJS.enc.Base64.stringify(hash);
// document.write(hashInBase64);
//authorization = 'SharedKey ' + customerId +':'+ hashInBase64 ;
signature = CryptoJS.enc.Base64.stringify(CryptoJS.HmacSHA256(CryptoJS.enc.Utf8.parse(stringToHash), CryptoJS.enc.Base64.parse(sharedkey)));
authorization = 'SharedKey ' + customerId + ':' + signature;
uri = "https://" + customerId + ".ods.opinsights.azure.com" + resource + "?api-version=2016-04-01";
var myHttp = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");
debugger;
$.ajax({
url: uri,
type: 'POST',
success: function (data) {
//do something to data
},
beforeSend: function (xhr) {
xhr.setRequestHeader('Authorization', "SharedKey " + customerId + ":" + signature);
xhr.setRequestHeader('x-ms-date', msdate);
xhr.setRequestHeader('x-ms-version', '2014-02-14');
xhr.setRequestHeader('Accept-Charset', 'UTF-8');
xhr.setRequestHeader("Access-Control-Allow-Origin", "*");
//xhr.setRequestHeader('Accept', 'application/json;odata=nometadata');
xhr.setRequestHeader("Log-Type", logType);
xhr.setRequestHeader("time-generated-field", TimeStampField);
xhr.setRequestHeader("Content-Type", "application/json; charset=utf-8");
//xhr.setRequestHeader('DataServiceVersion', '3.0;NetFx');
//xhr.setRequestHeader('MaxDataServiceVersion', '3.0;NetFx');
},
datatype:'json',
//contentType: 'application/json',
data:encodebody,
error: function (rcvData) {
console.log("ERRor");
console.log(rcvData);
}
});
var strBody={
“主机名”:“SDFSDF”,
“客户”:“sdfsfdsfdsf”,
“RoundTripLatencyInMs”:67,
};
调试器;
customerId=“XXXXXXXXXXXXX”;
sharedkey=“xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx”;
logType=“xxxxxxxxx”;
TimeStampField=“YYYY-MM-DDThh:MM:ssZ”;
//encodebody=stringToUtf16ByteArray(JSON.stringify(strBody.toString('utf8'))//encode_utf8(JSON.stringify(strBody));
str=strBody;
var byteArray=[];
对于(变量i=0;i if(str.charCodeAt(i)当您使用javascript从浏览器端进行REST调用时,浏览器将首先通过OPTIONS
方法向资源发送一个HTTP请求,以查找CORS头。请参阅。日志分析REST API不允许OPTIONS
HTTP动词。这意味着API不支持CORS。因此,它引发了您的问题
因此,您应该使用服务器端语言(如PHP、Python、Node.js等)调用RESTAPI