异步HTTP(ajax)请求在脚本标记中有效,但在js文件中无效
asynchronous HTTP (ajax) request works in script tag but not in js file
我在页面底部的脚本标记中有这个ajax调用。一切都很好!我可以在控制器中的"updatestatus"操作方法中设置断点。我的服务器也被发布了,这个方法被称为很棒!但是,当我将javascript放入js文件中时,ajax调用不会影响我的服务器。不过,里面的所有其他代码都在运行,只是没有对studentcontroller updatestatus方法进行ajax post调用。
<script>
$(document).ready(function () {
console.log("ready!");
alert("entered student profile page");
});
var statusdropdown = document.getElementById("enumstatus");
statusdropdown.addEventListener("change", function (event) {
var id = "@Model.StudentId";
var url = '@Url.Action("UpdateStatus", "Student")';
var status = $(this).val();
$.post(url, { ID: id, Status: status }, function (data) {
// do something with the returned value e.g. display a message?
// for example - if(data) { // OK } else { // Oops }
});
var e = document.getElementById("enumstatus");
if (e.selectedIndex == 0) {
document.getElementById("statusbubble").style.backgroundColor = "#3fb34f";
} else {
document.getElementById("statusbubble").style.backgroundColor = "#b23f42";
}
}, false);
</script>
现在我把这个放在我的页面底部。
@section Scripts {
@Scripts.Render("~/bundles/studentprofile")
}
在我的bundle.config文件中,它看起来像这个
bundles.Add(new ScriptBundle("~/bundles/studentprofile").Include(
"~/Scripts/submitstatus.js"));
submittatus.js看起来是这样的。我知道它输入并运行这个代码,因为我看到了警报消息和背景颜色的变化。所以代码正在运行。它只是没有发回我的服务器。
$(document).ready(function () {
console.log("ready!");
alert("submit status entered");
var statusdropdown = document.getElementById('enumstatus');
statusdropdown.addEventListener("change", function (event) {
var id = "@Model.StudentId";
var url = '@Url.Action("UpdateStatus", "Student")';
var status = $(this).val();
$.post(url, { ID: id, Status: status }, function (data) {
// do something with the returned value e.g. display a message?
// for example - if(data) { // OK } else { // Oops }
});
var e = document.getElementById('enumstatus');
if (e.selectedIndex == 0) {
document.getElementById("statusbubble").style.backgroundColor = "#3fb34f";
} else {
document.getElementById("statusbubble").style.backgroundColor = "#b23f42";
}
}, false);
});
在控制台窗口中,我收到了此错误消息。
岗位https://localhost:44301/Student/@Url.操作(%22更新状态%22,%20%22学生%22)404(未找到)
Razor代码未在外部文件中解析,因此在主视图中使用var id = "@Model.StudentId";
将产生(例如)var id = 236;
,在外部脚本文件中,它将产生var id = '@Model.StudentId';
(值未解析)
您可以在主视图中声明变量
var id = "@Model.StudentId";
var url = '@Url.Action("UpdateStatus", "Student")';
外部文件将能够访问这些值(从外部脚本文件中删除以上2行),或者将它们添加为元素的data-
属性,例如(我假设enumstatus
是一个下拉列表?)
@Html.DropDownListFor(m => m.enumStatus, yourSelectList, "Please select", new { data_id = Model.StudentId, data_url = Url.Action("UpdateStatus", "Student") })
它将呈现类似的东西
<select id="enumStatus" name="enumStatus" data-id="236" data-url="/Student/UpdateStatus">
然后在外部文件脚本中,您可以访问值
var statusbubble = $('#statusbubble'); // cache this element
$('#enumStatus').change(function() {
var id = $(this).data('id');
var url = $(this).data('url');
var status = $(this).val();
$.post(url, { ID: id, Status: status }, function (data) {
....
});
// suggest you add/remove class names instead, but if you want inline styles then
if (status == someValue) { // the value of the first option?
statusbubble.css('backgroundColor', '#3fb34f');
} else {
statusbubble.css('backgroundColor', '#b23f42');
};
});
相关文章:
- jquery代码在Mozilla中有效,但在其他浏览器上无效
- 滚动在Chrome中有效,但在Firefox或IE中无效
- 为什么这个按钮在IE中有效,但在Firefox中无效
- Joomla模式框弹出代码在文章中有效,但在代码中无效
- jQuery"焦点”;在一个实例中有效,但在其他实例中无效
- javascript/jQuery:在title中添加换行符——在IE9中有效,但在IE8中无效
- Javascript下载在firefox中停止,但在Chrome中有效
- MagnificPopupLightbox在RubyonRails开发中有效,但在部署到Heroku时无效
- JavaScript函数在Safari中有效,但在Firefox中找不到
- 获取所选选项的Javascript在Webkit/FF中有效,但在IE中无效
- 转换链接在jsfiddle中有效,但在我的浏览器上无效
- ComboBox typeAhead有效,但在某些行为条件下valueField为空
- 为什么这在 jsfiddle 中有效,但在我的文档中不起作用
- MySql数据库INSERT操作在硬盘(localhost)上有效,但在Internet上托管时无效
- 输入元素模式属性的Javascript正则表达式在reFiddle上有效,但在页面上无效
- 在Rails中使用remote:true在Chrome中有效,但在Safari中无效——从js.erb文件调用succe
- slideUp命令被忽略,但在控制台中有效
- 正则表达式在浏览器上有效,但在Node.js中无效
- 禁用字段在IE中有效,但在Firefox中无效
- 在getJSON之后构建HTML在Safari中有效,但在Chrome或Firefox中无效