jQuery的阿贾克斯未能来电MVC 4控制器的方法 [英] jQuery Ajax failing to call to MVC 4 Controller method

查看:119
本文介绍了jQuery的阿贾克斯未能来电MVC 4控制器的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用jQuery,以便点击某个按钮之后,火了一个Ajax调用。我读过的语法和可能遇到的问题的几个例子,但一直没有找到我的事业的有效的解决方案。这里的code。

I'm attempting to use jQuery in order to fire off an Ajax call after clicking a certain button. I've read several examples of the syntax and issues that may be encountered, but have failed to find a working solution for my cause. Here's the code.

控制器的方法:(HomeController.cs)

Controller Method: (HomeController.cs)

    [HttpPost]
    public JsonResult ChangeCompany(string companyCode)
    {
        return Json(new { result = companyCode }, JsonRequestBehavior.AllowGet);
    }

jQuery的code:

jQuery Code:

    function changeCompany(company) {
    $.ajax({
        url: '@Url.Action("ChangeCompany", "Home")',
        type: 'POST',
        data: JSON.stringify({ companyCode: company }),

        success: function (data) {
            alert("Company: " + data);
        },
        error: function (req, status, error) {
            alert("R: " + req + " S: " + status + " E: " + error);
        }
    });
}

最后,我调用这个函数:

And finally, I'm calling this function with:

$('.companyButton').click(function () {
    compCode = $(this).text();
    debug("Click event --> " + $(this).text());
    changeCompany(compCode);
});

我的调试信息显示正常,Ajax调用不断失败,出现以下警告: R:[对象的对象] S:误差E:未找到

我不完全知道该怎么做了这一点。

I'm not entirely sure what to make of that.

我知道有关于这个主题的几个问题,但他们都不来解决我的问题,我真的不知道有什么不对这些code块。任何有识之士将AP preciated。

I know there are several questions on this topic, but none of them seem to resolve my issue and I'm honestly not sure what's wrong with these code blocks. Any insight would be appreciated.

编辑: 在情况下,它是值得注意,这为移动设备。测试在Windows 8 Phone模拟器(Internet Explorer)中,沿着jQuery Mobile的。不知道是否会影响阿贾克斯在所有

In case it's worth noting, this is for a mobile device. Testing on Windows 8 Phone Emulator (Internet Explorer), alongside jQuery Mobile. Not sure if that affects Ajax at all

编辑2: 服用后一看原始网络电话,似乎'Url.Action(ChangeCompany,家),不被转换为正确的网址是而不是直接调用就好像它是原始URL文本。这是由于一个过时的jQuery,或其他一些因素?

EDIT 2: After taking a look at the raw network call, it seems that 'Url.Action("ChangeCompany", "Home")' is not being converted into the proper URL and is instead being called directly as if it were raw URL text. Is this due to an outdated jQuery, or some other factor?

推荐答案

确定您的EDIT2看来你是使用 URL:@ Url.Action(ChangeCompany,家), 在一个单独的JavaScript文件。你可以只写.cshtml文件中的剃须刀code,它是不工作的.js文件里面

Ok with your EDIT2 it seems you are using url: '@Url.Action("ChangeCompany", "Home")', in a separate JavaScript file. you can only write the razor code inside the .cshtml file and it is not working inside the .js files

这篇关于jQuery的阿贾克斯未能来电MVC 4控制器的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆