角$ HTTP表单系列化 [英] angular $http form serialization

查看:113
本文介绍了角$ HTTP表单系列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用$ HTTP的角度发布表单数据到控制器...我想张贴序列化的数据..所以我寻求关于发布数据的序列帮助...因为数据:$('#主) .serialize()不工作对我来说...

i am using $http in angular to post form data to controller... i want to post serialized data.. so i am seeking help about serialization of posting data... because data: $('#main').serialize() is not working for me...

demoapp.factory("authser", function ($location,$http) {
    return {
        login: function (credantials) {
            if (credantials.username != "jot") {
                alert("its ");
            }
            else {
               // var formdata = $('#main').serialize();
               //console.log(formdata);

                $http({
                    method: 'POST',
                    url: "/valued/newvalue",
                    //responseType: "json",
                    data: $('#main').serialize()
                    //data: { 'name': credantials.username, 'password': credantials.password }
                }).success(function (data) {
                    console.log(data);
                    $location.path('/logout');
                });
            }
        },

我的控制器

using angulardata.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Data.Entity;

namespace angulardata.Controllers
{
    public class valuedController : Controller
    {
        // GET: /valued/

        public ActionResult newvalue(Blog blog)
        {

            using (BlogContext ctx = new BlogContext())
            {
                Blog n = new Blog();
                ctx.Blogs.Add(blog);
                ctx.SaveChanges();
            }

            var res = new { Success = "True", Message = "Error Message" };
            return Json(res,JsonRequestBehavior.AllowGet);



        }

    }
}

但它不工作..任何帮助

but it is not working.. any help

推荐答案

当然,这是行不通的,你shoudl张贴其中包含的表单输入绑定到数据$范围对象:

Of course that won't work, you shoudl be posting the $scope object which contains the data that the form inputs are bound to:

$scope.formData={};

<input type="text" ng-model="formData.firstName" />
<input type="text" ng-model="formData.lastName" />

   demoapp.factory("authser", function ($location,$http,formData) {
    return {
        login: function (credantials) {
            if (credantials.username != "jot") {
                alert("its ");
            }
            else {
                $http({
                    method: 'POST',
                    url: "/valued/newvalue",
                    data: formData
}
                }).success(function (data) {
                    console.log(data);
                    $location.path('/logout');
                });
            }
        },

这篇关于角$ HTTP表单系列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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