jQuery ajax,url属性无法正常工作

时间:2013-06-29 13:44:49

标签: javascript jquery ajax

我使用这个简单的代码,但它无法正常工作(我的想法):

$(document).on("click", "#summary_and_tables #tables ul li a", function() {
    var url = "index.php/table/show/"+this.hash;
    console.log(url);
    $.ajax({
        type: "POST",
        url: url,
        dataType: 'json',
        success: function(response) {
            if(response.status == 'ok') {}
        }
    });
});

控制台中的输出是:

index.php/table/show/#summary

但是ajax请求发送到:

http://test.loc/st_base/index.php/table/show/

2 个答案:

答案 0 :(得分:2)

不幸/幸运的是,没有错。事情按预期工作。浏览器只是不向服务器发送哈希。 如果你真的需要传递它,请将其放入数据中:

var url = "index.php/table/show/";
var hashOnly = this.hash;
$.ajax({
    type: "POST",
    data: {myhash: hashOnly},
    url: url,
    dataType: 'json',
    success: function(response)
    {
        if(response.status == 'ok')
        {
        }
    }
});

答案 1 :(得分:0)

您正在使用相对网址,这会将index.php/table/show/"+this.hash附加到您脚本的当前位置。

您的脚本似乎位于“http://test.loc/st_base/

尝试指定完整网址,以“http://mycorrectsite.com/blah/index.php/table/show ...”

开头