【问题标题】:Ajax get call url relative to current oneAjax 获取相对于当前的调用 url
【发布时间】:2014-06-04 21:50:53
【问题描述】:

我无法理解我的 ajax 调用在做什么。我在几个不同的页面中使用了一个局部视图,它们的调用方式相同。

function showChoice(divID) {
    $.get('/Add/Examples/' + divID, function (data) {
        $('#partialExamples').html(data);
    });
}

这在本地主机上运行良好,但现在我正在尝试将此应用程序移至服务器,其中 url 将以 http://apps/test/ 开头。

url 的这个test 部分搞砸了我的ajax 调用。所有其他路由都保留完整的 url 开头并将路由添加到结尾,例如 http://apps/test/add 等。但是这些 ajax 调用会尝试转到 http://apps/add/examples 并完全摆脱 url 中的 test 部分。

我已经尝试过add/examples(所以没有第一个斜杠),但这会将这个 url 添加到当前 url 的末尾,而不是返回到起始 url。

因此,如果我在 http://apps/test/review 上调用此函数,它将转到 https//:apps/add/examples 而不是 https//:apps/test/add/examples

有什么想法吗?

已解决:

$.get('@Url.Action("PduExamples", "AddPdu", new { id = "_ID" })'.replace("_ID", divID)

【问题讨论】:

    标签: jquery ajax asp.net-mvc-4


    【解决方案1】:

    改变

    $.get('/Add/Examples/' + divID, function (data) {
    

    $.get('@Url.Action("Examples", "Add")' + divID, function (data) {
    

    【讨论】:

    • 知道了!感谢您的超快速答复!
    【解决方案2】:

    你可以使用

    <base href="YourOptionalRout" />
    

    在html中修改ajax的相对路径

    【讨论】:

      猜你喜欢
      • 2012-08-28
      • 2019-01-06
      • 2023-03-13
      • 2012-06-24
      • 1970-01-01
      • 2021-05-07
      • 1970-01-01
      • 1970-01-01
      • 2012-11-12
      相关资源
      最近更新 更多