[英]Ajax call works in IE and Chrome (and only in Firefox when adding breakpoint in debug mode at end of ajax call)
I currently have the following code that works in IE and Chrome. 我目前有以下在IE和Chrome中运行的代码。 The following issue is only within Firefox (which weirdly enough I can make it work in Firefox when in Debug mode and I add a breakpoint at the end of the ajax call shown in comments below). 以下问题仅在Firefox中存在(很奇怪,在调试模式下,我可以使它在Firefox中运行,并在下面的注释所示的ajax调用的末尾添加一个断点)。 I believe it might have something to do with the ajax call below (where it errors). 我相信这可能与下面的ajax调用有关(错误)。 (SEE CODE COMMENTS FOR DETAILS). (有关详细信息,请参见代码注释)。
I have the following code that receives the data from the forms and sends it to another JavaScript function to send the data back to MVC controller function that adds a user into a database. 我有以下代码,这些代码从表单接收数据并将其发送到另一个JavaScript函数,以将数据发送回MVC控制器函数,后者将用户添加到数据库中。
$('#addUserSubmit').click(function () {
var tagId = $(this).closest('form').find('input[name="userTag"]').val();
var adminUser = $(this).closest('form').find('input[name="userName"]').val();
AddUser(tagId, adminUser);
});
function AddUser(tagId, adminUser) {
var data = JSON.stringify({
'tagId': tagId,
'adminUser': adminUser
});
$.ajax({ //HERE IS WHERE THE ERROR OCCURS (After it goes through to set all of the variables below).
type: "POST",
url: "/UsersAccounts/AddUser",
data: data,
success: function (outVal) {
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert(XMLHttpRequest + textStatus + errorThrown);
},
contentType: 'application/json'
}); //IF I ADD A BREAKPOINT HERE IT WILL WORK IN GOING TO THE MVC
//FUNCTION TO BELOW AND IT WILL WORK AS INTENDED (BUT IF I RUN
//THE CODE NORMALLY OR WITHOUT A BREAKPOINT HERE IT WILL ALERT
//WITH THE ERROR ABOVE).
}
[HttpPost]
public String AddUser(int tagId, string adminUser){
if (tagId > 0){
try{
using (TubeBPC db = new TubeBPC()){
Users user = new Users() {TAGID=tagId, AdminUser=adminUser };
db.Users.Add(user);
db.SaveChanges();
return "success";
}
}
catch(Exception dbe){
throw dbe;
}
}
else{
throw new ArgumentOutOfRangeException("TagID is not a positive number");
}
}
Any help is much appreciated! 任何帮助深表感谢!
I managed to do more research and found this answer to be the solution. 我设法进行了更多研究,并找到了解决方案。
"In order to wrap this code in a function the returns a boolean and does not require a callback function (a blocking procedure) you will need to use a synchronous request" “为了将此代码包装在一个函数中,它返回一个布尔值并且不需要回调函数(阻塞过程),您将需要使用同步请求”
jquery ajax dont work without firebug break point 没有firebug断点,jQuery ajax无法正常工作
Hope it helps someone else! 希望它能帮助别人!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.