[英]MVC Jquery Ajax Request error 0x800a138f - JavaScript runtime error: Object expected
I have been struggling with this function for the past hour now and i am not sure what is going wrong, I have a function that needs to call a controller action and populate a dropdown, the code seems to fail on the line where I declare the url, it fails with undefined and throw the exception above, any idea how i can fix this, any help will be very appreciated. 我在过去一个小时内一直在努力使用此函数,但我不确定出了什么问题,我有一个需要调用控制器动作并填充下拉列表的函数,代码似乎在我声明网址,它失败,并没有定义,并抛出上述异常,任何想法我可以解决这个问题,任何帮助将不胜感激。
Here is my dropdown definition 这是我的下拉定义
<div class="form-group">
@Html.LabelFor(m => m.Town, new { @class = "col-md-2 control-label" })
<div class="col-md-10">
@Html.DropDownList("Town", ViewBag.Town as SelectList)
</div>
</div>
<div class="form-group">
@Html.Label("Surburb", new { @class = "col-md-2 control-label" })
<div class="col-md-10">
@Html.DropDownList("Surburb", new SelectList(string.Empty, "Value", "Text"), "Please select a Surburb", new { style = "width:250px", @class = "dropdown1" })
</div>
</div>
And here is my Jquery request 这是我的Jquery请求
<script type="text/javascript" src="~/Scripts/jquery-1.10.2.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$("#Town").change(function () {
debugger;
var url = url("GetSurburbs", "Account");
$.post(url,{id : $("#Town").val()}).done(function(data){
alert('Success');
}).fail(function(data){
alert('failed');
});
})
})
</script>
It should be the Url.Action
html helper method. 它应该是
Url.Action
html helper方法。
var url = '@Url.Action("GetSurburbs", "Account")';
Assuming this code is inside a razor file. 假设此代码位于剃刀文件中。
This will execute the Url.Action
and set the return value, which is the correct relative path to the GetSurburbs
action method to the url
variable. 这将执行
Url.Action
并设置返回值,该返回值是GetSurburbs
url
变量的GetSurburbs
操作方法的正确相对路径。
The above code will work when it is inside a razor file because the razor file get executed on the server. 上面的代码在剃须刀文件中时将起作用,因为剃须刀文件在服务器上执行。 But If you do not prefer to make the html helper method calls in your js code, you can keep this url value in the view markup and read from there in your js as needed.
但是,如果您不希望在js代码中进行html helper方法调用,则可以将此url值保留在视图标记中,并根据需要在其中从js中读取。
@Html.DropDownList("Surburb", new List<SelectListItem>(), "Please select a Surburb",
new { style = "width:250px", @class = "dropdown1",
data_url=Url.Action("GetSurburbs","Account") })
This will generate the data-url attribute to your SELECT element.Now simply read this value and use in your javascript code. 这将为您的SELECT元素生成data-url属性。现在只需读取该值并在您的javascript代码中使用即可。
var url = $("#Surburb").data("url");
You defining url incorrectly. 您定义的网址不正确。 You need to use @Url.Action extension
您需要使用@ Url.Action扩展
<script type="text/javascript" src="~/Scripts/jquery-1.10.2.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$("#Town").change(function () {
debugger;
var url = '@Url.Action("GetSurburbs", "Account")';
$.post(url,{id : $("#Town").val()}).done(function(data){
alert('Success');
}).fail(function(data){
alert('failed');
});
})
})
</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.