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.

我尝试使用jQuery,以便在单击某个按钮后触发Ajax调用。我已经阅读了一些可能遇到的语法和问题的示例,但是没有找到一个有效的解决方案。这里的代码。

Controller Method: (HomeController.cs)

控制器方法:(HomeController.cs)

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

jQuery Code:

jQuery代码:

    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);
});

My debug message displays properly, and the Ajax call constantly fails with the following alert: R: [object Object] S: error E: Not Found

我的调试消息显示正确,并且Ajax调用在以下警告下不断失败:R:[对象]S: error E: Not Found

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

我不太清楚这是怎么回事。

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.

我知道关于这个主题有几个问题,但是没有一个能解决我的问题,我真的不知道这些代码块有什么问题。任何见解都将受到赞赏。

EDIT: 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

编辑:如果值得注意,这是一个移动设备。在Windows 8手机模拟器(Internet Explorer)和jQuery手机上进行测试。不确定这是否会影响Ajax

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?

编辑2:在查看原始网络调用后,似乎是“Url”。Action(“ChangeCompany”,“Home”)并没有被转换成正确的URL,而是被直接调用,就好像它是原始的URL文本一样。这是因为过时的jQuery还是其他原因?

3 个解决方案

#1


6

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

好的,对于EDIT2,您似乎使用的是url: '@Url。Action(“ChangeCompany”,“Home”),在一个单独的JavaScript文件中。您只能在.cshtml文件中编写razor代码,并且它不能在.js文件中工作

更多相关文章

  1. python (9)统计文件夹下的所有文件夹数目、统计文件夹下所有文件数
  2. 当函数在单独的PHP文件中定义时,调用JavaScript函数onclick按钮事
  3. 使用Python写HTML 文件使用jinja2中的模板
  4. 计算机视觉相关代码片段(Python)
  5. 120行python代码解锁10000分微信跳一跳
  6. XGBoost中参数调优的完整指南(含Python-3.X代码)
  7. Python读取修改ini配置文件[ConfigParser]
  8. 求助:Python是否可以用一行代码来同时给变量赋值并打印变量的值
  9. 用户输入从.csv文件生成新列表?

随机推荐

  1. Android studio 快捷键
  2. Android AsyncTask
  3. Android(安卓)StudioDebug调试详细大全
  4. 2014年开源夏令营-android下使用libhackr
  5. android如何对应用进行单元测试
  6. Android夸进程通信机制八:使用 AIDL进行进
  7. Android夸进程通信机制六:使用ContentProv
  8. Android开发:原生+H5之:Android webview配
  9. Android(安卓)原生定位
  10. Crawling the Android(安卓)Marketplace