我在角度工厂中使用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);
}
}
}
答案 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');
});