2014-02-19 94 views
-2

我使用$ http角度将表单数据发布到控制器...我想发布序列化数据..所以我正在寻求有关发布数据序列化的帮助......因为数据:$( '#main')。序列化()不工作对我来说...

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); 



     } 

    } 
} 

,但它不工作..任何帮助

+0

到底是怎么回事呢?这个问题有可能因为不清楚而关闭。 stackoverflow.com/help/how-to-ask –

+0

我编辑问题SIR @KevinBrown –

回答

0

当然是赢得没有用,你应该发布$ scope对象其中包含了数据的表单输入绑定到:

$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'); 
       }); 
      } 
     }, 
+0

我想发布序列化数据.... –

+0

请参阅编辑帖子 –

+0

@ Jatt.net,你试图在jQuery中使用角度数据办法。这个答案是正确的方法。你应该阅读[如何“认为AngularJS”如果我有一个jQuery的背景是什么?(http://stackoverflow.com/questions/14994391/how-do-i-think-in-angularjs-if-i-have -a-jquery-background) – Liam