简体   繁体   English

jQuery getJSON url

[英]jQuery getJSON url

I am setting up a twitter feed into my site and have it working perfectly when you access it with a root page ie www.domain.com/index.php 我正在为我的网站设置一个Twitter提要,当你通过根页访问它时,它可以正常运行,即www.domain.com/index.php

However when you have a url that is www.domain.com/category/page it doesn't not work, however if you use www.domain.com/index.php?cat=category&page=page it works fine. 但是当你有一个www.domain.com/category/page的网址时,它不起作用,但是如果你使用www.domain.com/index.php?cat=category&page=page它可以正常工作。

So in the javascript I have the following 所以在javascript中我有以下内容

jQuery(function() {
    jQuery.getJSON('cache/twitter-json.txt', function(data){
    jQuery.each(data, function(index, item){
    jQuery('#tweet').append('code for displaying tweet');
});
});

So I have tried to change the url in the getJSON function to the full path www.domain.com/cache/twitter-json.txt but that doesn't work. 所以我试图将getJSON函数中的url更改为完整路径www.domain.com/cache/twitter-json.txt,但这不起作用。

Try: 尝试:

jQuery.getJSON('/cache/twitter-json.txt', function(data){

Relative URLs are interpreted relative to the directory of the page containing the reference. 相对URL相对于包含引用的页面目录进行解释。 So when you make the above call from www.domain.com/category/page , it tries to go to www.domain.com/category/cache/twitter-json.txt . 因此,当您从www.domain.com/category/page拨打上述电话时​​,会尝试访问www.domain.com/category/cache/twitter-json.txt

If you want to use the full URL, you need to put // before the hostname, eg 如果要使用完整的URL,则需要在主机名前添加// ,例如

jQuery.getJSON('//www.domain.com/cache/twitter-json.txt', function(data){

Otherwise, it thinks www.domain.com/ is just another level of directory. 否则,它认为www.domain.com/只是另一个级别的目录。

change 更改

jQuery.getJSON('cache/twitter-json.txt', function(data){

to

jQuery.getJSON('/cache/twitter-json.txt', function(data){

也许在您的html中设置基数会有所帮助

<base href="http://myserver/dir1/">

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

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