-2

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

my controller

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

Jot Dhaliwal
  • 1,470
  • 4
  • 26
  • 47
  • What exactly is going on? This question is in danger of being closed for being unclear. stackoverflow.com/help/how-to-ask – Kevin Brown Feb 19 '14 at 16:32

1 Answers1

0

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" />

and

   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');
                });
            }
        },
Mohammad Sepahvand
  • 17,364
  • 22
  • 81
  • 122
  • @Jatt.net, your trying to use angular data in a jquery way. This answer is the correct approach. You should read [How do I “think in AngularJS” if I have a jQuery background?](http://stackoverflow.com/questions/14994391/how-do-i-think-in-angularjs-if-i-have-a-jquery-background) – Liam Feb 19 '14 at 14:20
  • @Liam yes i solve the issue in angulary way... its cool , mohammad answer 95% help me. also i make some edit to the mohammad answer ,,,thnx – Jot Dhaliwal Feb 21 '14 at 07:39