How to create json file with an array created in cmd running js project?
Im working in a puppeteer js scrapping project that is runn开发者_开发问答ing in Windows terminal "node file.js". I get an array as my scrapping result, but now I need to use it in php. I need to make a file for php to read or to send the data directly to my php file.
Any ideas?
I have tried AJAX but I get this: ReferenceError: XMLHttpRequest is not defined
Also I have tried to create the file in my js project using:
new File([provincias], "xScrap.json", { type: "application/json" });
and getting this result: ReferenceError: File is not defined
For why there is an error of ReferenceError: XMLHttpRequest
it is for web browsers, and node js has another way of creating HTTP requests:
XMLHttpRequest module not defined/found
To send or request data to your PHP file or server, you need to create HTTP requests. I personally suggest using Axios, but there are a lot of alternatives here: How is an HTTP POST request made in node.js?
As for your creation of file error, I don't know why it isn't working, but I'll suggest using the fs module to create/read/update/delete files in node js: https://blog.risingstack.com/fs-module-in-node-js/
精彩评论