2014-02-19 95 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