Ajax调用不能与asp.net mvc一起使用

时间:2022-02-15 03:17:23

Below is my Ajax call.

下面是我的Ajax调用。

$.ajax({
    url: '@Url.Action("GetCities", "People")',
    type: 'GET',
    dataType: 'JSON',
    contentType: 'application/json; charset=utf-8',
    data: {'id' : id },
    success: function (cities) {
        alert('success');
    },
    error:function(err, result){
        alert('error');
    }
});

& this is my action method.

这是我的行动方法。

    public ActionResult GetCities(int id)
    {
        var cities = StateDB.GetCities().Where(c => c.StateId == id);
        return Json(cities, JsonRequestBehavior.AllowGet);
    }

When i try to access this using http://localhost/People/GetCities/2, it works, but it is not working with above ajax call. It gives me 404 'Not found' error. What am i doing wrong here?

当我尝试使用http:// localhost / People / GetCities / 2访问它时,它可以工作,但它无法使用上面的ajax调用。它给了我404'未找到'错误。我在这做错了什么?

2 个解决方案

#1


0  

The problem was external js file. Razor engine doesn't work with external js file, thus the url was not correct. It is working if i put the same code in view. Thanks to all who helped me.

问题是外部js文件。 Razor引擎不能与外部js文件一起使用,因此url不正确。如果我在视图中输入相同的代码,它会工作。感谢所有帮助过我的人。

#2


0  

It is actually the other way around: external JS files cannot work with Razor engine (that's quite obvious if you think about it). If you set a javascript variable to @Url.Action('','',) inside a view (= which is covered by the Razor engine), then you can reference this variable in your external script.

它实际上是另一种方式:外部JS文件无法与Razor引擎一起工作(如果你考虑它就很明显)。如果在视图(= Razor引擎覆盖的内容)中将javascript变量设置为@ Url.Action('','',),则可以在外部脚本中引用此变量。

Inside a view:

在视图内:

<script>
var ajaxUrl = '@Url.Action("GetCities", "People")';
</script>

Inside your external script (assuming the script above is referenced as well):

在您的外部脚本中(假设上面的脚本也被引用):

$.ajax({
url: ajaxUrl,
type: 'GET',
dataType: 'JSON',
contentType: 'application/json; charset=utf-8',
data: {'id' : id },
success: function (cities) {
    alert('success');
},
error:function(err, result){
    alert('error');
}

});

It's not the best practice (because your external script has a dependency on a variable that might or might not exist).

这不是最佳实践(因为您的外部脚本依赖于可能存在或可能不存在的变量)。

#1


0  

The problem was external js file. Razor engine doesn't work with external js file, thus the url was not correct. It is working if i put the same code in view. Thanks to all who helped me.

问题是外部js文件。 Razor引擎不能与外部js文件一起使用,因此url不正确。如果我在视图中输入相同的代码,它会工作。感谢所有帮助过我的人。

#2


0  

It is actually the other way around: external JS files cannot work with Razor engine (that's quite obvious if you think about it). If you set a javascript variable to @Url.Action('','',) inside a view (= which is covered by the Razor engine), then you can reference this variable in your external script.

它实际上是另一种方式:外部JS文件无法与Razor引擎一起工作(如果你考虑它就很明显)。如果在视图(= Razor引擎覆盖的内容)中将javascript变量设置为@ Url.Action('','',),则可以在外部脚本中引用此变量。

Inside a view:

在视图内:

<script>
var ajaxUrl = '@Url.Action("GetCities", "People")';
</script>

Inside your external script (assuming the script above is referenced as well):

在您的外部脚本中(假设上面的脚本也被引用):

$.ajax({
url: ajaxUrl,
type: 'GET',
dataType: 'JSON',
contentType: 'application/json; charset=utf-8',
data: {'id' : id },
success: function (cities) {
    alert('success');
},
error:function(err, result){
    alert('error');
}

});

It's not the best practice (because your external script has a dependency on a variable that might or might not exist).

这不是最佳实践(因为您的外部脚本依赖于可能存在或可能不存在的变量)。