Javascript HTTP Post不工作
单击index.html代码中显示的按钮会显示调用$http.post的错误,网页会发出“error!”警报,因此,我无法保存新的JSON文件,因为我无法通过此调用 我做错了什么 index.htmlJavascript HTTP Post不工作,javascript,jquery,angularjs,Javascript,Jquery,Angularjs,单击index.html代码中显示的按钮会显示调用$http.post的错误,网页会发出“error!”警报,因此,我无法保存新的JSON文件,因为我无法通过此调用 我做错了什么 index.html <td><input id="clickMe" type="button" value="clickme" ng-click="doPost()" /></td> index.js服务器 var fs = require('fs'); //File syste
<td><input id="clickMe" type="button" value="clickme" ng-click="doPost()" /></td>
index.js服务器
var fs = require('fs'); //File system module
server.post('/resource', function(request, response)
{
//Every HTTP post to baseUrl/resource will end up here
console.info('Updated myJSON.json');
fs.writeFile('myJSON.json', request.body, function(err)
{
if(err)
{
console.error(err);
return response.status(500).json(err); //Let the client know something went wrong
}
console.info('Updated myJSON.json');
response.send(); //Let the client know everything's good.
});
});
使用$http like的then函数
$http.post('/resource', {param1: 1, param2: 2}).then(function successCallback(response) {
// this callback will be called asynchronously
// when the response is available
}, function errorCallback(response) {
// called asynchronously if an error occurs
// or server returns response with an error status.
});
怎么了?对服务器的请求是您期望的吗?服务器的响应是什么?错误是我收到了警报:error!。当我使用HTTP.post时没有成功,它应该用OK!提醒我!。是的,您收到该警报是因为您编写了显示该警报的代码。然而,错误!与错误消息得到的结果一样没有帮助。那么,代码忽略的实际错误是什么?我怀疑有一个函数参数被传递给了那个。错误回调,那个参数是什么?在浏览器调试工具的“网络”选项卡中,服务器的实际响应是什么?对不起,我是新手。无论如何,Chrome上的控制台告诉我mywebsite.org/resource 404找不到。尝试了这个,仍然没有结果。然后你的nodejs api出现问题,$http像这样工作,$http没有问题
$http.post('/resource', {param1: 1, param2: 2}).then(function successCallback(response) {
// this callback will be called asynchronously
// when the response is available
}, function errorCallback(response) {
// called asynchronously if an error occurs
// or server returns response with an error status.
});