AngularJS http 发送帖子请求失败 [英] AngularJS http sending post request fails

查看:33
本文介绍了AngularJS http 发送帖子请求失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有了这个 GET 请求,它就可以工作了(将请求发送到服务器并且服务器能够处理它):

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")
        })

当我使用这个 POST 请求时,它没有.

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")
                })

或以不同的形式书写:

           $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")
            });

我最后得到的是:

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

请求的payload是json:{"name":"name1","surname":"surname1"}

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

在服务器(playframework)上,我定义了应该接受客户端调用的路由.

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)

或者没有":jsonForm"

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

可能是什么原因.我错过了什么?

What might be the reason. What I missed?

更新

有趣的是:在我的笔记本电脑上使用它后(请参阅下面的答案),然后将它推送到 gitHub 并将其拉到另一台机器上,它开始以不同的方式工作.现在它抱怨 Bad Request 是 [Invalid XML] 不过我使用 "application/json" 标头并且在提交后没有更改任何代码行.我想知道这可能是一个错误.

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.

似乎沿着 DocumentType,我需要提供 dataType : 'json',但因为 Play 希望它在 url 中.这一切都行不通.

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

推荐答案

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

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

这里是:Playframework 处理发布请求

请参阅那里的更新 2 部分.

重点是:

  1. 在 Angular 方面:构建 url 时根本不需要使用参数.data
  2. 的一般方法
  3. 在服务器端使用 Play' bodyParser 来提取数据,无论通过什么作为请求正文.
  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.

这篇关于AngularJS http 发送帖子请求失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆