jQuery从节点js发出请求(用于将JSON文件上传到myjson.com)

时间:2020-11-29 16:02:51

I am a beginner in javascript. I am able to get json object from http://myjson.com/api in my html page using ajax by using:

我是javascript的初学者。我可以使用ajax在我的html页面中从http://myjson.com/api获取json对象:

$.getJSON("https://api.myjson.com/bins/bjm28", function (data) {
    $.each(data, function (index, value) {
       console.log(index,value);
    });
});

I wish to update the same json file with different values from my app.js which I run using nodejs.

我希望用我使用nodejs运行的app.js中的不同值更新相同的json文件。

I tried https://www.npmjs.com/package/ajax-request and "Update a json" column in http://myjson.com/api

我在http://myjson.com/api上试过https://www.npmjs.com/package/ajax-request和“Update a json”专栏

var request = require('ajax-request')
data={
    "Man":960,
    "Woman":40
}
request({
        url:'https://api.myjson.com/bins/bjm28',
        type:'PUT',
        dataO:data,
        contentType:"application/json; charset=utf-8",
        dataType:"json",
        success: function(dataO, textStatus, jqXHR){

        }
    }); 

All the methods I find are used for javascript running in browser (not nodejs)

我找到的所有方法都用于在浏览器中运行的javascript(不是nodejs)

Is it possible for me to update this json file through nodejs? I am trying myjson because initially I was unable to load local json/txt to html pages due to "Cross origin request errors.." My aim to control my html webpage through nodejs (lotion.js) for dapp.

我可以通过nodejs更新这个json文件吗?我正在尝试myjson,因为最初我无法将本地json / txt加载到html页面,因为“Cross origin request errors ..”我的目标是通过nodejs(lotion.js)为dapp控制我的html网页。

1 个解决方案

#1


0  

For your specific question/example you can use the NPM Request module: https://www.npmjs.com/package/request

对于您的具体问题/示例,您可以使用NPM请求模块:https://www.npmjs.com/package/request

You should do the following:

您应该执行以下操作:

let request = require("request");

let requestOptions = {
    // your options here
};

request.get("https://www.example.com", requestOptions, function(error, response, body)
{
    // process the response here
};

In the Request module documentation you can also find examples to POST information.

在Request模块文档中,您还可以找到POST信息的示例。

Hope this can help!

希望这可以帮助你!

#1


0  

For your specific question/example you can use the NPM Request module: https://www.npmjs.com/package/request

对于您的具体问题/示例,您可以使用NPM请求模块:https://www.npmjs.com/package/request

You should do the following:

您应该执行以下操作:

let request = require("request");

let requestOptions = {
    // your options here
};

request.get("https://www.example.com", requestOptions, function(error, response, body)
{
    // process the response here
};

In the Request module documentation you can also find examples to POST information.

在Request模块文档中,您还可以找到POST信息的示例。

Hope this can help!

希望这可以帮助你!