简体   繁体   English

外部jquery文件上的帖子不起作用

[英]A post on a external jquery file doesn't work

Im using ASP.NET MVC5 with Jquery 3.3.1, before, i have the post on the same page of the view inside of an script tag and it works perfectly. 我在Jquery 3.3.1中使用ASP.NET MVC5,之前,我在脚本标记内的视图的同一页面上发布了帖子,它工作得很好。 But i heard is better have your functions in a external jsfile. 但是我听说最好在外部jsfile中使用您的函数。

the post is for get a simple array of data and fill a datatable. 该职位是为了获取简单的数据数组并填充数据表。

the others functions on my jsfile are working good, but my post is returning me a 404 not found, i put a breakpoint on my JsonAction but the post dont even call it. 我的jsfile上的其他函数运行良好,但是我的帖子向我返回了一个未找到的404,我在JsonAction上放置了一个断点,但帖子甚至没有调用它。

Sorry for my bad English 对不起,我的英语不好

This is my POST function: 这是我的POST函数:

var url = '@Url.Action("GetCardsData","Card")';

$.post(url).done(function (data) {
    var request = JSON.parse(data);

    var i = 0;
    var exist = false;


    request.filter(function (el) {

        el.filter(function (ul) {

            array[i] = ul;


            i++;


        })

    });

My JsonAction: 我的JsonAction:

 public JsonResult GetCardsData(string CardNumber) {
        if (CardNumber == null) {
                            CardNumber = "";
        }
        int? Reference_ID = User.Reference_ID;
        Card c = CardRepo.GetCard(Company.Company_ID, CardNumber);

        new AdmSql();
        var cardListq = @"SELECT * FROM  [DEF].[GetCardsData] ({0}, 3)";
        var cardList = DEFRepo.ParseList(DaSql.Query(cardListq, Reference_ID));
        var data = JsonConvert.SerializeObject(cardList);


        return Json(data);
    }

this is the error 404 这是错误404

Most probably your var url = '@Url.Action("GetCardsData","Card")'; 最有可能您的var url = '@Url.Action("GetCardsData","Card")'; code is in .js file. 代码在.js文件中。 It should not be there, since Razor cannot detect it there, so your Url finally looks as is, so 404 (Not Found) because there is no @Url.Action("GetCardsData","Card") , but rather there is /Card/GetCardsData . 它不应该在那里,因为Razor无法在此处检测到它,所以您的Url最终看起来像是,所以404(未找到),因为没有@Url.Action("GetCardsData","Card") ,而是/Card/GetCardsData Therefore, put this line inside of your .cshtml page like so: 因此,请将此行放入.cshtml页面中,如下所示:

<script>
    var url = '@Url.Action("GetCardsData","Card")';
</script>

Now, your url will be correct. 现在,您的网址将是正确的。

The problem is that ' @Url.Action ' will not work in external JS Files . 问题是' @ Url.Action '在外部JS文件中不起作用。 It can be called from .cshtml or .vbhtml files only. 只能从.cshtml或.vbhtml文件中调用它。

But if you still want to make a call from external JS File then you should simply pass '/Card/GetCardsData' in url. 但是,如果您仍然想从外部JS文件进行调用,则只需在url中传递'/Card/GetCardsData'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM