所以我发送一个ajax请求给我的控制器进行处理。 但当我在我的ajax done函数中调试消息时,它会显示两个结果。
console.log的结果是:
{"result_code":1,"result_message":"Contact tags deleted","result_output":"json"}
{"result_code":1,"result_message":"Contact tags added","result_output":"json"}
这是ajax请求:
$('.travelSwitch').click(function () {
//send ajax request
$.ajax({
method: "POST",
url: "@Url.Action("ProcessPreferences", "ManagePreferences")",
data: {
//Applies to camping category
categoryTag: "Travel",
//send user email for processing
userEmailAddr: "@userEmailAddr",
//send remove flag for processing
removeTag: ($('#field_22Travel:checked').length) ? false : true
}
})
.done(function (msg) {
console.log(msg);
})
});
然后我的控制器中的函数处理ajax请求:
[HttpPost]
public ActionResult ProcessPreferences(string categoryTag, string userEmailAddr, bool removeTag)
{
string applyToTag = "TOPIC: " + categoryTag;
string emailAddr = userEmailAddr;
bool removeFlag = removeTag;
var acs = new Acs("api_key", "api_url");
Dictionary<string, string> getParameters = new Dictionary<string, string>();
if (removeFlag == false) {
//get parameters
getParameters.Add("api_action", "contact_tag_add");
getParameters.Add("api_output", "json");
}
else
{
//get parameters
getParameters.Add("api_action", "contact_tag_remove");
getParameters.Add("api_output", "json");
}
//post parameters
Dictionary<string, string> postParameters = new Dictionary<string, string>
{
{ "email", emailAddr },
{ "tags", applyToTag }
};
var response = acs.SendRequest("POST", getParameters, postParameters);
return Content(response);
}
因此,当处理ajax请求时,它会向活动的广告系列API发送请求。然后根据以后添加或删除标签,选中是否选中复选框。
NOTES 该开关来自此实现:https://www.w3schools.com/howto/howto_css_switch.asp
在页面加载时发送初始ajax请求以将开关设置为打开或关闭:
$.ajax({
method: "POST",
url: "@Url.Action("InitPreferences", "ManagePreferences")",
data: {
userEmailAddr: "@userEmailAddr",
apiCallInit: true
}
})
.done(function (msg) {
//loop through tags
for (i in msg.tags) {
//console.log(msg.tags[i]);
(msg.tags[i] == "TOPIC: Camping") ? $("#field_22Camping").attr("checked", true) : "";
(msg.tags[i] == "TOPIC: Hiking") ? $("#field_22Hiking").attr("checked", true) : "";
(msg.tags[i] == "TOPIC: Travel") ? $("#field_22Travel").attr("checked", true) : "";
}
})
答案 0 :(得分:2)
似乎您的ajax函数被调用了两次,并且在两次调用之间复选框未被选中...请在您尝试点击它时验证您的元素订阅了多少事件,它是&#39 ;更多的是javascript问题,而不是后端的imo。
答案 1 :(得分:0)
好的,我明白了。
有了Wail所说的,我使用e.preventDefault();
不止一次地停止了事件绑定。
然后因为我通过javascript设置复选框属性我只是用javascript更改它们的值。
这是我的更新代码:
$('.travelSwitch').click(function (e) {
e.preventDefault();
if ($('.travelSwitch input').attr("checked")) {
var remTag = false;
$('.travelSwitch input').attr("checked", false);
}
else {
var remTag = true;
$('.travelSwitch input').attr("checked", true);
}
//send ajax request
$.ajax({
method: "POST",
url: "@Url.Action("ProcessPreferences", "ManagePreferences")",
data: {
//Applies to camping category
categoryTag: "Travel",
//send user email for processing
userEmailAddr: "@userEmailAddr",
//send remove flag for processing
removeTag: remTag
}
})
.done(function (msg) {
console.log(msg);
})
});
希望这有助于其他人:)