AngularJS http sending post request fails

2020-05-04 05:17发布

问题:

Having this GET request, it works (sends request to the sever and server is able to handle it):

    /* post to server*/
    $http.get("/forms/FormValidator1/validateForm/" + JSON.stringify(data)).
        success(function(data) {
            console.log("good")
        }).
        error(function(data, status, headers, config) {
            console.log("something wrong")
        })

When I use this POST request it doesn't.

 $http.post("/forms/FormValidator1/validateForm/" + JSON.stringify(data)).
                success(function(data) {
                    console.log("good")
                }).
                error(function(data, status, headers, config) {
                    console.log("something wrong")
                })

or writing in different form:

           $http({
                url: '/forms/FormValidator1/validateForm',
                method: "POST",
                data: JSON.stringify(data),
                headers: {'Content-Type': 'application/json'}
            }).success(function (data, status, headers, config) {
                    console.log("good")
            }).error(function (data, status, headers, config) {
                    console.log("something wrong")
            });

What I have in the end is:

Request URL:http://localhost:9000/forms/FormValidator1/validateForm
Request Method:POST
Status Code:404 Not Found

Requested payload is json: {"name":"name1","surname":"surname1"}

On the server (playframework) I defined routes that are supposed to accept client calls.

GET        /forms/FormValidator1/validateForm/:jsonForm           controllers.FormValidator1.validateForm(jsonForm:String)

POST        /forms/FormValidator1/validateForm/:jsonForm            controllers.FormValidator1.validateForm(jsonForm:String)

Or with no ":jsonForm"

 POST        /forms/FormValidator1/validateForm            controllers.FormValidator1.validateForm(jsonForm:String)

What might be the reason. What I missed?

UPDATE

Interestly enough: after I got it working on my laptop (see my answer below) then push it on gitHub and pull it to another machine it starts working differently. Now it complains than Bad Request is [Invalid XML] nevertheless I use "application/json" header and did not change any line of code after commit. I wonder maybe it is a bug.

It seems along DocumentType, I need provide dataType : 'json', but since Play wants it be in the url. This all does not work.

回答1:

The server configuration accepts POST requests on URLs of the form: /forms/FormValidator1/validateForm/:jsonForm.

The URL the client is POSTing to is/forms/FormValidator1/validateForm/, which does not fit that form (note the missing :jsonForm).



回答2:

I've came up with an answer related to playframework, which fixes my/this issue.

Here it is: Playframework handling post request

See UPDATE 2 section there.

The point is:

  1. On Angualar side: No need to use params at all while building url. Just general approach with data
  2. And on server side use Play' bodyParser to extract data, whatever was passed as a body of request.