简体   繁体   English

MVC Jquery Ajax请求错误0x800a138f-JavaScript运行时错误:预期对象

[英]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操作方法的正确相对路径。

Using the HTML Helpers with external javascript files 将HTML帮助程序与外部javascript文件一起使用

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.

相关问题 0x800a138f-JavaScript运行时错误:无法获取未定义或空引用的属性“ split” - 0x800a138f - JavaScript runtime error: Unable to get property 'split' of undefined or null reference 0x800a138f - JavaScript运行时错误:无法获取未定义或空引用的属性“mData” - 0x800a138f - JavaScript runtime error: Unable to get property 'mData' of undefined or null reference 创建一个MVC4项目-0 0x800a1391-JavaScript运行时错误:“ jQuery”未定义”错误 - Create an MVC4 project -0 0x800a1391 - JavaScript runtime error: 'jQuery' is undefined” Error “ 0x800a1391-JavaScript运行时错误:&#39;jQuery&#39;未定义”错误 - “0x800a1391 - JavaScript runtime error: 'jQuery' is undefined” Error 0x800a1391-JavaScript运行时错误:&#39;jQuery&#39;未定义 - 0x800a1391 - JavaScript runtime error: 'jQuery' is undefinedd 0x800a1391 - JavaScript运行时错误:&#39;jQuery&#39;未定义 - 0x800a1391 - JavaScript runtime error: 'jQuery' is undefined 异常0x800a1391-JavaScript运行时错误:在ASP.NET MVC中未定义&#39;JSON&#39; - Exception 0x800a1391 - JavaScript runtime error: 'JSON' is undefined in asp.net mvc 在母版页正文加载时调用jQuery函数:0x800a1391-JavaScript运行时错误:函数未定义 - Calling jQuery function on Master Page body onload: 0x800a1391 - JavaScript runtime error: function is undefined IE 10引发“ 0x800a1391-JavaScript运行时错误:&#39;jQuery&#39;未定义”(ASP.NET) - IE 10 Throws “0x800a1391 - JavaScript runtime error: 'jQuery' is undefined” (ASP.NET) 0x800a01b6-JavaScript运行时错误:对象不支持属性或方法“ cookie” - 0x800a01b6 - JavaScript runtime error: Object doesn't support property or method 'cookie'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM