简体   繁体   中英

Cannot access value from JSON file received from $http.post

I have this problem. I have grocery list item app written in AngularJS. I symulated a connection to server through AJAX request made to local files written in JSON format.

I have for example one file which returns me a fake serwer status:

[{
    "status": 1
}]

I try to receive that value of this status by writing code:

groceryService.save = function(entry){

        var updatedItem = groceryService.findById(entry.id);
        //We update the existing item 
        if(updatedItem){


            $http.post("data/updated_status.json", entry)

                .success(function(data){

                  //HERE IS PROBLEM ------------------- 
                  if(data.status == 1){
                  //END OF PROBLEM --------------------

                    updatedItem.completed = entry.completed;
                    updatedItem.itemName = entry.itemName;
                    updatedItem.date = entry.date;
                   }
            })

                .error(function(data,status){

            });
   //We creating new item 
   else{

   }

As I see I cannot access this status value and I don't know why. In Chrome Browser I don't receive any error code. Apparently as I see AngularJS must send back this JSON data in changed format which I must access differently, but how ?

I thought that AngularJS parses value 1 from int to string but I made small try and It failed.

I attach of course $http service in my service directive:

app.service("GroceryService", function($http){

I run my files on xampp local server.

If someone could help me solve this problem it would be great :-).

Thanks in advance.

EDITED: -------------------------------------------

I found out that the problem is with $http.post() AngularJS method. When I switch in my code to get() method everything work well.

I get error 404 from chrome browser when I trigger $http.post(). My code looks like this:

$http.post('data/ai.json', entry)

And this:

$http.post('data/update_status.json', entry)

Thirst I thought I made a mistake in file name so I changed it to ai.json but it is not a problem. I don't know where might be the problem, I use brackets IDE and it has option live preview of written code in brower. It creates some serwer to do this.

I attach also screenshot of directory of my files and screenshot of error 404 from chrome.

Still I need help in solving this problem. Thanks in advance :-).

Error 404 from Chrome when I click save button.

Directory of files. In data folder are all my .json files

During searching in Internet for solution I found this one:

$http.post("data/update_status.json", entry, {
    headers: { 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'}
  })

But I implemented it in my code and got the same Error 404 result. I am hoping for some help :-).

EDITED -------------------------------------------

Hi

I still didn't solved this error issue. I uploaded my files to coding ground on tutorialspoint to find out if it is not server issue. When I invoke $http.post() method I still get Error 404. I am 100% sure I wrote correct paths to files.

Can anyone help me in solving this. The obvious conclusion is that the error is in my code because server of coding ground should work well.

Any help folks ? :-)

Thanks

文件中的构造看起来像一个数组,因此您可能想尝试下面的代码。

if(data[0].status == 1)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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