HTTP Post无效

时间:2017-03-08 19:04:28

标签: javascript jquery angularjs

点击index.html代码中显示的按钮会给我一个错误的调用“$ http.post”,(网页警告“错误!”,所以我无法将新的JSON文件保存为我无法通过这个电话。

我做错了什么?

的index.html

<td><input id="clickMe" type="button" value="clickme" ng-click="doPost()" /></td>

app.js

...
$scope.doPost = function()
                        {
                            $http.post('/resource', {param1: 1, param2: 2}
                            ).success(function()
                                {
                                    alert("OK!");
                                })
                                .error(function()
                                {
                                    alert("Error!"); 
                                });
                        }
...

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.
        });
});

1 个答案:

答案 0 :(得分:1)

使用$ 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.
  });