File . json is not created in Angularjs

Asked

Viewed 387 times

4

I created a controller and a service. Follow them, to exemplify what is 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 from success on the Chrome console, but the form content is not written to the file .json, which has the 999 me preset ID as specified on the line event,id = 999; service. I have checked the path and permissions in the file system and both are correct. The strange thing is that to get the message from success in the browser console, I had to create the file 999.json in the file system, because before this, when the method save was executed, it returned error 404, URL not found (I found strange, because if I want to make a post, because it is done a get before?)

  • you can start your control as an anonymous function instead of: ... function EditEventController($scope, eventData){ ..., use function($scope, eventData) {...

  • Play in a variable before sending the resource. It’s a request order question, I believe. You first get the full url, then you get the request. You can’t do both at the same time... there’s no way you can get an id of something that hasn’t existed yet.

1 answer

1

Error 404 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 are related to all HTTP methods.

That’s why, even when you created the resource 999.json on your server, in the path specified, it no longer returns 404 status (I believe it has returned 200).

The problem is therefore in its implementation on the server.

HTTP

In your comment, you said you have nothing on the server.

The HTTP protocol, used in web requests (either HTTP 1, 1.1 or 2), has some access methods. The most used in traditional applications are GET and POST, but with the diffusion of the development of Restful Apis, the PUT and DELETE methods have also been widely used.

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

  1. GET: asks for representation of a particular resource;
  2. POST: asks the webserver to store the sent data subject to the specified resource (think of it as "changing the resource");
  3. PUT: asks for data to be stored as the specified resource (think "create a resource");
  4. DELETE: asks for the resource to 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://: schematic;
  • www.dominio.com.br: domain;
  • /meu/recurso: resource path;
  • ?consulta=exemplo: consultation;
  • #fragmento: fragment.

By default, HTTP does not specify what this "resource" is on the server: it is completely within 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).

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

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

So the correct thing would be to use PUT in your request? The correct thing would be to use PUT, not POST. But only this will not solve.

HTTP nothing but standardizes the exchange of messages between the client and the server. Web servers, like Nginx, Apache and any other, only implements this message exchange.

It’s up to you to implement how to respond to each of the messages. And it’s up to you to follow the status standards set by HTTP.

But why does GET work? Because when you install the server, you specify a path called DocumentRoot, which is (simply) a folder in the file system understood as the / of your 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 creating the archive 999.json you now receive a status 200.

But note that, despite everything, your POST requests probably did not change the resource itself. 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 yes perform due work.

How to do it? Each language is one way, and the chosen server (Apache, etc.) influences the implementation. Jetbeans should be JSP, look in Google. I already did, but I can’t even remember how he created the project. It’s not difficult, 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). As much as there are countless people out there developing for the web, the vast majority (no doubt!) do everything anyway, do not know the "why". Understanding the protocol (just the basics, calm down!) is the first step to becoming a good developer!

I hope I’ve helped!

  • Vinicius, thank you for your return. Actually, I don’t have anything on the server, I’m just testing with an HTML that calls JS files from inside it. I develop using Jetbeans Webstorm. I have no market server, such as IIS, Nodejs, Apache. The Webserver engine is provided by Webstorm itself. Any suggestions?

  • This is the problem. I will edit the question about the basic functioning of HTTP.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.