.json file is not created in angularJS

4

I created a controller and a service. Here are some examples of what's happening:

Controller

eventsApp.controller('EditEventController',
function EditEventController($scope, eventData){

    $scope.event = {};

    $scope.saveEvent = function (event,newEventForm){
        console.log(newEventForm);
        if (newEventForm.$valid){
            eventData.save(event)
                .$promise.then(
                    function(response) {console.log('success',response)},
                    function(response) {console.log('failure',response)}
            );
        }
    };

    $scope.cancelEdit = function (){
        window.location = "EventDetails.html";
    }
});

Service

eventsApp.factory('eventData', function($resource) {
var resource = $resource('data/event/:id'+'.json', {id:'@id'});
return {
    getEvent: function () {
        return resource.get({id:1});
    },
    save: function (event) {
        event.id = 999;
        return resource.save(event);
    }
};
});

The strange thing is that the message is success in the Chrome console, but the content of the form is not written to the file .json , which has the ID prefixed by me with 999 as specified in line event,id = 999; of the service. I have already checked the path and permissions on the file system and both are correct. The strange thing is that to get the message success in the browser console, I had to create the file 999.json in the file system, because before that, when the save method was executed, it returned URL error 404 not found (I found it strange, because if I am wanting to make a post, why is a get done before?)

    
asked by anonymous 03.05.2015 / 21:56

1 answer

1

The 404 error occurs because the specified resource does not exist on the server (or a handler for the resource). 404 errors are not only related to the GET method, but to all HTTP methods.

So even when you created the 999.json feature on your server, in the specified path, it stopped returning status 404 (I believe it has gone back to 200).

The problem, therefore, is in its implementation on the server.

HTTP

In your comment, you said that you do not have anything on the server.

The HTTP protocol used in Web requests (whether HTTP 1, 1.1 or 2) has some access methods. The most commonly used in traditional applications are GET and POST, but with the spread of RESTful APIs, the PUT and DELETE methods are also widely used.

These names are not just cute names. They have a semantics. Simplified:

  • GET: requests a representation of a particular resource;
  • POST: requests the webserver to store the submitted data subordinate to the specified resource (think as "change resource");
  • PUT: requests data to be stored as the specified resource (think as "create a resource");
  • DELETE: requests that the resource be removed.
  • What is a recurso ? Resource is specified in the URL after the domain and before the fragment and query and identifies "something" on the server. For example:

    http://www.dominio.com.br/meu/recurso?consulta=exemplo#fragmento
    
    • http:// : schema;
    • www.dominio.com.br : domain;
    • /meu/recurso : resource path;
    • ?consulta=exemplo : query;
    • #fragmento : fragment.

    By default, HTTP does not specify what this "resource" is on the server: it is completely from the domain of your web application. That is, it can be a file, a record in the database, or something much more complex (or simpler).

    Likewise, virtually all status codes can be returned by each of these methods. Often, these statuses also depend on the application domain.

    That is, a POST request can return a 404 if the resource is not found. A PUT however, is more unusual to return 404, since it is expected to not exist on the server and you want to create it.

    So the correct thing would be to use PUT in your request? The most correct would be to use PUT instead of POST. But that alone will not solve.

    HTTP only standardizes the exchange of messages between the client and the server. Web servers, such as Nginx, Apache and any other, only implements this messaging exchange.

    It is up to you to implement how to respond to each of the messages. And it's up to you to follow the HTTP-defined status patterns.

    But why does GET work? Because when you install the server, you specify a path named DocumentRoot , which is (simplified) a folder in the file system understood as the / of its resources. All requests that arrive on the server will be processed relative to this directory. By default, web servers deliver files (GET request) if they exist (status 200), or return error otherwise (404, 403, etc). That's why when you created the file 999.json you were given a 200 status.

    But note that, after all, your POST probably requests did not properly alter the feature. This is because although the resource exists, the server does not know what to do with it.

    So what to do? You should implement your resource handler for this feature, and it does the work due.

    How to do it? Each language is one way, and the chosen server (Apache, etc.) influences the implementation. JetBeans should be JSP, search on Google. I already did, but I do not even remember how I created the project. It's not hard not, and it's usually a boring and repetitive service.

    I hope the explanation has cleared up a bit. Take a look at the Wikipedia page on HTTP (in English). Although there are many people out there developing for the web, the vast majority (no doubt!) Does it all anyway, does not know the "porques". Understanding the protocol (just the basics, calm down!) Is the first step to becoming a good developer!

    I hope I have helped!

        
    04.05.2015 / 00:25