0

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.

4

2 回答 2

1

我想出了一个与 相关的答案playframework,它解决了我/这个问题。

这里是: Playframework 处理发布请求

请参阅此处的更新 2部分。

重点是:

  1. 在 Angular 方面:在构建 url 时根本不需要使用参数。只是一般的方法data
  2. 在服务器端使用Play'bodyParser来提取数据,无论是作为请求体传递的。
于 2013-11-13T02:52:42.987 回答
1

服务器配置接受以下形式的 URL 上的POST/forms/FormValidator1/validateForm/:jsonForm请求: 。

客户端发布到的 URL是/forms/FormValidator1/validateForm/,它不适合该表单(请注意缺少的:jsonForm)。

于 2013-11-12T20:31:24.837 回答