第二次单击时,ajax成功函数返回视图

时间:2014-02-19 05:57:05

标签: jquery ajax angularjs asp.net-mvc-4 angularjs-factory

我在角度工厂中使用ajax的帖子请求....一切正常,除了ajax成功函数...你可以看到成功函数我必须去另一页..(logout.html)..但是ajax的成功功能在点击登录按钮两次后登录到第二页,我不知道有什么问题需要帮助

我的角色工厂的ajax调用

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

                    }
                });
            }
        },
        logout: function (data) {
console.log(data);

            $location.path('/hello');
        }
    }
})

我的控制器

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace angulardata.Controllers
{
    public class valuedController : Controller
    {
       // GET: /valued/
        [HttpPost]
        public ActionResult newvalue(string name, string password)
        {
            var res = new {Success="True",Message="Error MEssage" };
            return Json(res);
        }

    }
}

1 个答案:

答案 0 :(得分:1)

而不是使用$.ajax尝试使用$http角度服务来调用将触发角度摘要周期的服务器。

demoapp.factory("authser", function ($location,$http) {

//

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