【问题标题】:Ajax error when there's a parameter in urlurl中有参数时出现Ajax错误
【发布时间】:2017-09-04 05:49:36
【问题描述】:

我有一个填充下拉列表的 ajax 代码,我使用 mvc c#。 当我从 ajax 调用我的方法并且我在方向栏中有一个没有参数的 url 时,代码可以正常工作,但是如果我在方向栏中的 url 中有一个参数,这不起作用并出现此错误: “{readyState: 4, getResponseHeader: ƒ, getAllResponseHeaders: ƒ, setRequestHeader: ƒ, overrideMimeType: ƒ, ...}”。

这是我的代码:

$.ajax({
    url:"../Records/myList",
    type: "POST",
    dataType: 'json',
    contentType: 'application/json',
   // data: JSON.stringify(Data),
    success: function (resul) {
        Function(resul);
    },
    error: function (message) {
    }
});

我的网址:http://localhost:123/Record/EditRecord ->> 成功了

我的另一个网址:http://localhost:123/Record/EditRecord/1 ->> 不是这样的

提前致谢。

【问题讨论】:

  • 两个网址都是一样的。你得到什么错误
  • 抱歉,我已经编辑了第二个网址。谢谢。
  • http://localhost:123/Record/EditRecord/1 => 这个请求是通过 GET 方法进行的吗?我知道通过 $.ajax 使用各种 HTTP 方法的方法,但我需要确定您要使用哪种方法。

标签: c# jquery asp.net-mvc


【解决方案1】:

从给定的第二个 URL(不起作用)我假设您想使用带有 HttpGet 方法的 jQuery AJAX。 URL 模式与此路由匹配:

http://localhost:123/{controller}/{action}/{id}

其中id 被视为UrlParameter

因此您需要使用表示 URL 参数值的操作参数和数据,例如:

控制器

[HttpGet]
public ActionResult EditRecord(int id)
{
    // other stuff
}

视图 (JS)

$.ajax({
    url: "/Record/EditRecord",
    type: "GET",
    dataType: 'json', // response type
    contentType: 'application/x-www-form-urlencoded; charset=utf-8', // header content type
    data: { id: 1 },
    processData: true, // this is important to use in GET methods
    success: function (result) {
        Function(result);
    },
    error: function (message) {
        // throw error
    }
});

或者直接使用 URL 参数适用于 GET 方法而不指定 data 内容:

视图 (JS)

$.ajax({
    url: "Record/EditRecord/1",
    type: "GET",
    processData: true, // this is important to use in GET methods
    success: function (result) {
        Function(result);
    },
    error: function (message) {
        // throw error
    }
});

注意:使用jQuery.get 简化版:

$.get("/Record/EditRecord/1", function (result) {
          Function(result);
      }, "json").error(function (message) { // throw error 
      });

PS:如果您正在寻找使用 AJAX 的正确 POST 方法,这是 HTTP POST 请求的示例。

控制器

[HttpPost]
public ActionResult EditRecord(Record rec)
{
    // other stuff
}

视图 (JS)

$.ajax({
    url: "/Record/EditRecord",
    type: "POST",
    dataType: 'json', // response type
    contentType: 'application/json; charset=utf-8', // header content type
    data: JSON.stringify({ rec: { id: 1, name: 'XXXX', ... }}),
    success: function (result) {
        Function(result);
    },
    error: function (message) {
        // throw error
    }
});

参考:

Do GET, POST, PUT, DELETE in ASP.NET MVC with jQuery AJAX

【讨论】:

    【解决方案2】:

    我在代码中看不到任何错误,但如果您选择使用(ajax 函数的)数据属性,我建议尝试让参数在服务器上附带它的名称:

    {key1: 'value1', key2: 'value2'}
    

    或者你使用字符串查询:

    page?name=ferret&color=purple
    

    因为你刚刚说第一种方法有效,我认为不需要检查 POST/GET 方法。

    编辑:

    奖励给this

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-06-30
      • 2021-04-21
      • 2013-05-06
      • 2021-02-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多