Asp。NET MVC Ajax-Post表单和Ajax-Get
I'm stuck, who can help me out? In my LogOn.aspx View I've login controls (Username, Password, RememberMe) inside a FORM tag:
我被困住了,谁能帮我?在我登录。aspx视图我有登录控件(用户名,密码,记住)在一个表单标签内:
<form id="loginform" method="post" action="/Account/LogOn/">
Below that, I've a hidden DIV with a OPTION dropdownlist and a confirm-button, with a onclick_event:
下面是一个隐藏的DIV,其中有一个选项下拉列表和一个确认按钮,还有一个onclick_event:
$.ajaxSetup({ cache: false });
$.ajax({
type: "GET",
url: "/Account/SetCompanyAndContinue",
data: "{ 'id' : '1'}",
contentType: "application/json; charset=utf-8",
dataType: "json"
});
First, the user logs in. In jQuery, I post the login credentials via AJAX:
首先,用户登录。在jQuery中,我通过AJAX发布登录凭据:
var loginCred = new Object();
loginCred.Username = $('#userName').val();
loginCred.Password = $('#password').val();
loginCred.RememberMe = $('#rememberMe').checked;
var myJsonObject = JSON.stringify(loginCred);
$.ajaxSetup({ cache: false });
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "/Account/LogOnAjax/",
data: myJsonObject,
dataType: "json",
success: function(data) {
PostCredentialsSuccess(data);
}
});
This POST works perfect. The breakpoint at the Controller Action is hit by the debugger and returns a JSON object of data. I put this JSON data into the OPTION dropdownlist. This Option Dropdownlist is then presented to the user. Then, when the user clicks the confirm-button, a second AJAX call is made:
这篇文章是完美的。控制器动作的断点被调试器击中并返回一个JSON数据对象。我将这个JSON数据放入选项下拉列表。然后将这个选项下拉列表呈现给用户。然后,当用户单击confirm-button时,进行第二次AJAX调用:
$.ajaxSetup({ cache: false });
$.ajax({
type: "GET",
url: "/Account/SetCompanyAndContinue",
data: "{ 'id' : '1'}",
contentType: "application/json; charset=utf-8",
dataType: "json"
});
I would expect that the Controller Action named "SetCompanyAndContinue" gets hit:
我预计名为“SetCompanyAndContinue”的控制器操作会被命中:
[AcceptVerbs(HttpVerbs.Get)]
public ActionResult SetCompanyAndContinue(string id)
{
SessionAdapter.CustomerID = Convert.ToInt32(id);
return null;
}
But it ain't happening, instead, the default Controller Action get's hit the first time:
但是这并没有发生,相反,默认的控制器动作get在第一次命中:
public ActionResult LogOn()
{
return View();
}
BUT(!) the second time I click (the same) confirm-button the Controller Action finally hits [SetCompanyAndContinue].
但是(!)第二次单击(相同的)确认按钮时,控制器动作最终会点击[SetCompanyAndContinue]。
Can somebody tell me what I'm doing wrong? Many thanks in advance.
有人能告诉我我做错了什么吗?提前感谢。
6 个解决方案
#1
2
you need to pass the data property as a javascript object not as a string
您需要将数据属性作为javascript对象传递,而不是作为字符串。
$.ajax({ type: "GET",
url: "/Account/SetCompanyAndContinue",
data: ({id : 1}),
contentType: "application/json; charset=utf-8",
dataType: "json" });
i recommend to use firebug to look at the actual HTTP Request that do get send from jQuery, that way tiny mistakes like this get obvious very quickly.
我建议使用firebug查看从jQuery发送的实际HTTP请求,这样,像这样的小错误很快就会显现出来。
hth
hth
更多相关文章
- jquery不会对select/选项更改事件作出反应。
- jQuery 三级联动选项栏
- 使用Next设置选定的选项
- 使用AJAX将动态数据传递给mvc控制器
- 当您有很多选项时,GUI替代
- 如何使用meta在“NEW”选项卡中重定向
- 使用ui.router如何让一个控制器成为另一个控制器的孩子?
- 在新选项卡中打开下载的文件
- 使活动选项卡在模式下不起作用