jQuery AJAX请求中的相对URL和绝对URL [英] Relative vs. absolute urls in jQuery AJAX requests

查看:341
本文介绍了jQuery AJAX请求中的相对URL和绝对URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难让我的AJAX请求在登台服务器上工作.一切在我的开发机器上都能正常工作,但是一旦我上传了它,我所有的AJAX请求都停止了工作.我发现,如果我将相对网址(例如"index.php")更改为绝对网址(" http: //example.com/index.php )请求再次起作用,但我不明白为什么.

I'm having a hard time getting my AJAX requests to work on a staging server. It all worked fine on my development machine, but as soon as I uploaded it, all my AJAX requests stopped working. I found out that, if I change the relative urls (eg. "index.php") to absolute urls ("http://example.com/index.php") the requests work again, but I do not understand why.

示例请求:

jQuery.post('index.php', {id: 1234, action: 1, step: 1}, function(data) { /* something */ });

这不起作用,我什至没有出现在Firebug控制台中.但是,成功处理程序被称为,这非常令人困惑.

This does not work, I does not even show up in the firebug console. The success handler is called though, which is very confusing.

这很好用:

jQuery.post('http://example.com/index.php', {id: 1234, action: 1, step: 1}, function(data) { /* something */ });

有人可以解释为什么AJAX请求以这种方式表现吗? x_X

Can anybody explain why AJAX requests behave in this way? x_X

推荐答案

在您的第一个示例中,尝试在index.php之前添加/以强制其从根目录看.仔细检查以确保您的目录结构与index.php所在的位置完全相同.

Try adding a / before index.php in your first example to force it to look from root. Double check to make sure your directory-structures are exactly the same with regards to where index.php is.

这篇关于jQuery AJAX请求中的相对URL和绝对URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆