简体   繁体   English

使用JSON的AngularJs $ http.post请求

[英]AngularJs $http.post request using JSON

I want to create a post request using JSON file. 我想使用JSON文件创建发布请求。 It's working for $http.get request but don't work for $http.post. 它适用于$ http.get请求,但不适用于$ http.post。 My method for a get request is - 我获取请求的方法是-

$http.get('data/data1.json').success(function(res){
    $scope.myDataSet = res;     
})

& It's returning all the JSON data that I saved in Json file. &返回我保存在Json文件中的所有JSON数据。 Now I want to create a post request that saved the data in this Json file, The method that I used for this - 现在,我想创建一个将数据保存在此Json文件中的发布请求,用于此操作的方法是-

var obj = {
            Id: $scope.id,
            Name: $scope.name
        }

        $http({
            url: 'data/data1.json',
            dataType: 'json',
            method: 'POST',
            data: obj,
            headers: {
                "Content-Type": "application/json"
            }
        }).success(function(response){
            $scope.myDataSet = response;
        }).error(function(error){
            $scope.error = error;
        });

& the error shows in browser - POST http://localhost:3000/data/data1.json 404 (Not Found). &错误显示在浏览器中-POST http:// localhost:3000 / data / data1.json 404(未找到)。

Remote Address:[::1]:3000
Request URL:http://localhost:3000/data/data1.json
Request Method:POST
Status Code:404 Not Found
Response Headers
view source
Connection:keep-alive
Content-Length:29
Content-Type:text/html; charset=utf-8
Date:Sat, 21 Nov 2015 06:10:47 GMT
X-Content-Type-Options:nosniff
X-Powered-By:Express
Request Headers
view source
Accept:application/json, text/plain, */*
Accept-Encoding:gzip, deflate
Accept-Language:en-US,en;q=0.8
Connection:keep-alive
Content-Length:13
Content-Type:application/json
Cookie:connect.sid=s%3ALlxaAOKSIGfyAfJIkxrRqDp2yDR4mqmd.%2BY9r%2FcOUStykG4ut5yvleAK6PHZ5KqvHCOjUUY%2BmS%2Fs; _ga=GA1.1.242893980.1444535433
DNT:1
Host:localhost:3000
Origin:http://localhost:3000
Referer:http://localhost:3000/
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.86 Safari/537.36

How can I saved the object data in this Json file..Please help me. 如何将对象数据保存在此Json文件中。请帮助我。

If you are not using server and directly calling the .json file then it should only be called as GET method. 如果您不使用服务器并直接调用.json文件,则应仅将其作为GET方法调用。 All the files are retrieved by default using HTTP GET method. 默认情况下,使用HTTP GET方法检索所有文件。

Since it is a pure JSON file and is not served through Server or any server side code, it won't be able to handle other HTTP methods such as POST, DELETE etc other than GET. 由于它是纯JSON文件,并且不能通过服务器或任何服务器端代码提供,因此它将无法处理GET以外的其他HTTP方法,例如POST,DELETE等。

UPDATE - 更新-

In your comment, you mentioned you want to save this post data to .json file. 在您的评论中,您提到要将此帖子数据保存到.json文件。 If this is your intention then this can not be done using $HTTP, you will need to handle this on server side. 如果这是您的意图,则无法使用$ HTTP做到这一点,您将需要在服务器端进行处理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM