Javascript Azure功能(手动触发):发出post请求
我试图使用Azure函数(手动触发器)在javascript中发出post请求,但收到以下错误:Javascript Azure功能(手动触发):发出post请求,javascript,azure,http,post,azure-functions,Javascript,Azure,Http,Post,Azure Functions,我试图使用Azure函数(手动触发器)在javascript中发出post请求,但收到以下错误: mscorlib: One or more errors occurred. Error: Cannot find module 'xmlhttprequest' at Function.Module._resolveFilename (module.js:469:15) at Function.Module._load (module.js:417:25) at Modul
mscorlib: One or more errors occurred. Error: Cannot find module 'xmlhttprequest'
at Function.Module._resolveFilename (module.js:469:15)
at Function.Module._load (module.js:417:25)
at Module.require (module.js:497:17)
at require (internal/module.js:20:19)
以下是我的功能:
var XMLHttpRequest = require('xmlhttprequest').XMLHttpRequest;
var xhr = new XMLHttpRequest();
module.exports = function (context, input) {
context.log('The Request body is:', input);
context.done();
var url = "<myurl>";
xhr.open("POST", url, false);
xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhr.send(input);
};
var XMLHttpRequest=require('XMLHttpRequest')。XMLHttpRequest;
var xhr=new XMLHttpRequest();
module.exports=函数(上下文,输入){
log('请求主体为:',输入);
context.done();
var url=“”;
xhr.open(“POST”,url,false);
setRequestHeader(“内容类型”,“应用程序/x-www-form-urlencoded”);
发送(输入);
};
有什么想法吗?您需要先安装
xmlhttprequest
模块
如果您正在门户中运行它,请转到https://{yourfunctionappname}.scm.azurewebsites.net/DebugConsole
在cmd控制台中,输入cd./site/wwwroot
,
然后npm安装xmlhttprequest
如果您在本地开发,只需在function project文件夹中打开一个cmd,然后npm安装xmlhttprequest
对于这个错误
mscorlib:Error:EPERM:operation not allowed,在Object.fs.openSync(fs.js:641:18)的Object.fs.writeFileSync(fs.js:1347:33)的send(D:\home\site\wwwroot\node\u modules\xmlhttprequest\lib\xmlhttprequest.js:477:10)的错误(本机)处打开“D:\Windows\system32\.node-xmlhttprequest-sync-7048”
您将open
方法异步模式设置为false
,这意味着此方法是同步执行的
它将在进程当前工作目录(cwd)中创建文件。在azure函数中,默认情况下它是D:\Windows\system32
,由于以下原因,我们无法在其中创建文件
如果您的函数运行时版本是beta版,我们可以使用process.chdir(“d:\\home\\site\\wwwroot\\functionname”)
将cwd更改为函数文件夹。一切都会好起来的
如果运行时为~1,则在更改cwd后,将出现新错误--无法按预期删除同步锁。节点xmlhttprequest sync xxxx
文件。所以,在一次执行之后,函数下次将被阻塞,并且无法响应
另一种选择是使用异步模式或尝试使用除
xmlhttprequest
以外的模块。您需要先安装xmlhttprequest
模块
如果您正在门户中运行它,请转到https://{yourfunctionappname}.scm.azurewebsites.net/DebugConsole
在cmd控制台中,输入cd./site/wwwroot
,
然后npm安装xmlhttprequest
如果您在本地开发,只需在function project文件夹中打开一个cmd,然后npm安装xmlhttprequest
对于这个错误
mscorlib:Error:EPERM:operation not allowed,在Object.fs.openSync(fs.js:641:18)的Object.fs.writeFileSync(fs.js:1347:33)的send(D:\home\site\wwwroot\node\u modules\xmlhttprequest\lib\xmlhttprequest.js:477:10)的错误(本机)处打开“D:\Windows\system32\.node-xmlhttprequest-sync-7048”
您将open
方法异步模式设置为false
,这意味着此方法是同步执行的
它将在进程当前工作目录(cwd)中创建文件。在azure函数中,默认情况下它是D:\Windows\system32
,由于以下原因,我们无法在其中创建文件
如果您的函数运行时版本是beta版,我们可以使用process.chdir(“d:\\home\\site\\wwwroot\\functionname”)
将cwd更改为函数文件夹。一切都会好起来的
如果运行时为~1,则在更改cwd后,将出现新错误--无法按预期删除同步锁。节点xmlhttprequest sync xxxx
文件。所以,在一次执行之后,函数下次将被阻塞,并且无法响应
另一种选择是使用异步模式或尝试使用除
xmlhttprequest
以外的模块,另一种选择是使用NodeJS的HTTP
模块-
在这种情况下,您可以从以下内容开始:
var http = require('http');
然后通过以下方式发送数据:
var req = http.request(options, function(response) {
var str = "";
response.on("data", function (chunk) {
str += chunk;
});
response.on("end", function () {
res.json(str);
});
});
另一个选项是使用NodeJS的
HTTP
模块-
在这种情况下,您可以从以下内容开始:
var http = require('http');
然后通过以下方式发送数据:
var req = http.request(options, function(response) {
var str = "";
response.on("data", function (chunk) {
str += chunk;
});
response.on("end", function () {
res.json(str);
});
});
抱歉,响应延迟,您解决了新错误?仍遇到以下错误。有什么想法吗?mscorlib:Error:EPERM:operation not allowed,在Object.fs.openSync(fs.js:641:18)的Object.fs.writeFileSync(fs.js:1347:33)的send(D:\home\site\wwwroot\node\u modules\xmlhttprequest\lib\xmlhttprequest.js:477:10)的错误(本机)处打开“D:\Windows\system32\.node-xmlhttprequest-sync-7048”–Kevin Ngo 6秒前编辑抱歉响应延迟,您解决了新错误?仍然遇到以下错误。有什么想法吗?mscorlib:Error:EPERM:operation not allowed,在Object.fs.openSync(fs.js:641:18)的Object.fs.writeFileSync(fs.js:1347:33)的send(D:\home\site\wwwroot\node\u modules\xmlhttprequest\lib\xmlhttprequest.js:477:10)的错误(本机)处打开“D:\Windows\system32\.node-xmlhttprequest-sync-7048”–Kevin Ngo 6秒前编辑