简体   繁体   English

Ajax调用在服务器上不起作用

[英]Ajax call not working on server

I have following code in my Javascript file: 我的Javascript文件中有以下代码:

$.getJSON("/ProductMatrix/LocationList/" + $("#categoryTypeFilter > option:selected").attr("value"),
  function (data) {
    var items = "<option> Default  </option>";
    $.each(data,
      function (i, location) {
        items += "<option value=' " + location.Value + "'>" + location.Text + "</option>";
      });
    $("#captureLocationFilter").html(items);
  });

"/ProductMatrix/LocationList/" is controller/action but its not working on server, however it does work on Local machine. "/ProductMatrix/LocationList/"是控制器/操作,但在服务器上不起作用,但是在本地计算机上起作用。 Looks like there is problem with url, as this path may not be existing on server. 网址似乎有问题,因为此路径可能在服务器上不存在。 Can someone help me on it. 有人可以帮我吗。

When I try Url.Action my external javascript file is not happy. 当我尝试Url.Action时,我的外部javascript文件不满意。

在此处输入图片说明

My wild guess is that you are getting a 404 error. 我的疯狂猜测是您遇到404错误。 Check your browser console. 检查您的浏览器控制台。

One suggestion is, do not hard code the url to your action method like that. 一个建议是,不要像这样将URL硬编码到您的操作方法中。 Always try to use the html helper methods like Url.Action or Url.RouteUrl to build the correct relative url to your action method. 始终尝试使用诸如Url.ActionUrl.RouteUrl类的html帮助器方法来为您的操作方法构建正确的相对URL。

var url="@Url.Action("LocationList","ProductMatrix")";
var v=$("#categoryTypeFilter > option:selected").attr("value");
//update the url (add ? as needed) according to how your server expects the url params
$.getJSON(url + v , function (data) {              
             //do something with data    
});

This will work fine if your javascript code is inside a razor view, If it is inside an external javascript file, use the solution explained in this answer . 如果您的javascript代码在razor视图中,则此方法会很好地工作;如果在外部javascript文件中,请使用此答案中说明的解决方案。

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

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