AngularJS发送HTTP POST请求失败 [英] AngularJS http sending post request fails

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

问题描述

有了这个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")
            });

我有什么到底是:

What I have in the end is:

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

请求的有效载荷是JSON: {名:名1,姓: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?

更新

Interestly不够:我得到了它在我的笔记本电脑工作后(见我的回答如下),然后将其推GitHub上,并将其拉到另一台机器就开始工作不同。现在,它抱怨比坏请求为[无效XML]不过我用应用/ JSON头并没有改变$ C $的任意行℃之后提交。我不知道,也许这是一个错误

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 看来,我需要提供数据类型:'JSON',但由于播放希望它在该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.

在这里:
<一href=\"http://stackoverflow.com/questions/19941786/playframework-handling-post-reqeust/19944146#19944146\">Playframework处理后reqeust

请参阅更新2 部分存在。

问题的关键是:


  1. 在Angualar方:没有必要使用PARAMS都同时建立网址。与数据只是一般的方法

  2. 而在服务器端使用播放 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 POST请求失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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