【发布时间】:2018-06-19 04:01:43
【问题描述】:
我正在使用包请求。你可以在 npm 中找到它。我多次尝试在我的 API 中添加数据。我的 API 来自 myjson.com/api。我真的需要帮助。我尝试了所有方法,例如 put,post,get ...。我只想将客户添加到 API 中。每次如果对象客户的值发生变化,它应该创建一个新行并将这个客户添加到。
例如:您有一个客户可以注册的网站。然后您将每次注册都保存在 API 中的新行中。
var customer = {
"Customerid": customerID,
"Sex": sex,
"Lastname": lastname,
"Firstname": firstname,
"State": state,
"Street": street,
"Postcode": postcode,
"Birthdate": birthdate,
"Email": email,
"Monthly Earnings": mothEarn
};
var customerData = JSON.stringify(customer);
addCustomerAPI(customerData);
function addCustomerAPI(customerData){
request({
url: "https://api.myjson.com/bins/1efewz",
type: "PUT",
data: customerData,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data, textStatus, jqXHR) {
console.log(data)
}
});
}
希望大家帮忙,我现在真的很难过。
最好的问候
迈克尔
【问题讨论】:
-
您是否遇到任何错误?
-
我发布的网页上没有任何内容。它在我运行应用程序后更新值,但在我的应用程序中它不起作用
-
你在js文件中导入模块了吗? @MichaelDev
var request = require('request') -
是的,它已经实现了
标签: javascript json api http request