在形式中没有价值 - AngularJS POST [英] No value in form - AngularJS POST

查看:103
本文介绍了在形式中没有价值 - AngularJS POST的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图做一个角形式的职位,但输入的 testValue 没有得到一个值。

有什么建议?

在angularJs控制器:

  // FORM
$ scope.submitEditSyncSettingsForm =功能(){
    如果($(#editSyncSettingsForm)。有效()){        $ HTTP({
            网址:数据/ postSyncSettings.aspx
            数据:{testValue:一些价值},
            方法:POST,
            标题:{内容类型:应用程序/ x-WWW的形式urlen codeD;字符集= UTF-8'}
        })。成功(功能(数据){
            的console.log(数据)
        })错误(功能(错误){ERR,执行console.log(ERR)})    }
};

的.aspx code后面

 公共部分类postSyncSettings:System.Web.UI.Page
{
    保护字符串strResponse =;    保护无效的Page_Load(对象发件人,EventArgs的发送)
    {
        strResponse =的Request.Form [testValue];
    }
}


解决方案

问题是数据仍然在身体JSON发送。你必须自己序列化。您可以使用 serializeData()从功能的这篇文章 ...

  $ scope.submitEditSyncSettingsForm =功能(){
    $ HTTP({
        网址:数据/ postSyncSettings.aspx
        数据:serializeData({testValue:一些价值'}),
        方法:POST,
        标题:{
            内容类型:应用程序/ x-WWW的形式urlen codeD;字符集= UTF-8
        }
    })。成功(功能(数据){
        的console.log(数据)
    })错误(功能(错误){
        ERR,执行console.log(ERR)
    })
};// http://www.bennadel.com/blog/2615-posting-form-data-with-http-in-angularjs.htm
功能serializeData(数据){
    //如果这不是一个对象,推迟到本土字符串化。
    如果(!angular.isObject(数据)){
        收益率((数据== NULL):data.toString());
    }    VAR缓冲= [];    //序列化对象中的每个关键。
    对于(数据变量名称){
        如果(!data.hasOwnProperty(名称)){
            继续;
        }        VAR值=数据[名]        buffer.push(
        EN codeURIComponent(名)+
            =+ EN codeURIComponent((价值== NULL):值?));    }    //序列化缓冲区并清理运输。
    无功源= buffer.join(与&)
        .replace(/%20 / g的,+);    返回(源);
}

I am trying to do a post of a form in angular, but the input testValue doesn't get a value.

Any suggestions?

In the angularJs controller:

//FORM
$scope.submitEditSyncSettingsForm = function () {
    if ($("#editSyncSettingsForm").valid()) {

        $http({
            url: "data/postSyncSettings.aspx",
            data: { testValue: 'some value' },
            method: 'POST',
            headers: { 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8' } 
        }).success(function (data) {
            console.log(data)
        }).error(function (err) { "ERR", console.log(err) })

    }
};

.aspx code behind

public partial class postSyncSettings : System.Web.UI.Page
{
    protected string strResponse = "";

    protected void Page_Load(object sender, EventArgs e)
    {
        strResponse = Request.Form["testValue"];
    }
}

解决方案

The problem is the data is still sent in the body as JSON. You'll have to serialize it yourself. You can use the serializeData() function from this article...

$scope.submitEditSyncSettingsForm = function () {
    $http({
        url: "data/postSyncSettings.aspx",
        data: serializeData({ testValue: 'some value' }),
        method: 'POST',
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
        }
    }).success(function (data) {
        console.log(data)
    }).error(function (err) {
        "ERR", console.log(err)
    })
};

// http://www.bennadel.com/blog/2615-posting-form-data-with-http-in-angularjs.htm
function serializeData(data) {
    // If this is not an object, defer to native stringification.
    if (!angular.isObject(data)) {
        return ((data == null) ? "" : data.toString());
    }

    var buffer = [];

    // Serialize each key in the object.
    for (var name in data) {
        if (!data.hasOwnProperty(name)) {
            continue;
        }

        var value = data[name];

        buffer.push(
        encodeURIComponent(name) +
            "=" + encodeURIComponent((value == null) ? "" : value));

    }

    // Serialize the buffer and clean it up for transportation.
    var source = buffer.join("&")
        .replace(/%20/g, "+");

    return (source);
}

这篇关于在形式中没有价值 - AngularJS POST的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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