72661

jQuery getJSON url

Question:

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

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.

So in the javascript I have the following

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.

Answer1:

Try:

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

Relative URLs are interpreted relative to the directory of the page containing the reference. 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.

If you want to use the full URL, you need to put // before the hostname, e.g.

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

Otherwise, it thinks www.domain.com/ is just another level of directory.

Answer2:

change

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

to

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

Answer3:

Maybe it helps to set the base in your html

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

Recommend

  • How to correctly get a WinForms Button control to draw custom text
  • How can I get a specific line from a text file? [duplicate]
  • How to read data from a text file if the file location is not known in c#?
  • Proguard Exception java.io.IOException: Duplicate zip entry
  • parsing numbers from a text file
  • Yii2: Using Kartik Depdrop Widget?
  • How to determine the CCSID used in CPYFRMIMPF command?
  • Reading XML into Datatable gives incorrect DateTime when the time has Time Zone info
  • How to save dynamically created textboxes and their values
  • php is_file always return false
  • pip in virtualenv gets ConnectTimeoutError
  • Run multiple queries from 1 SQL file showing result in multiple tables
  • Installed module is empty
  • Jenkins: How To Build multiple projects from a TFS repository?
  • HTML download movie download link
  • Sony Xperia Z Tablet not found by adb
  • How to recover from a Spring Social ExpiredAuthorizationException
  • Updating server-side rendering client-side
  • ILMerge & Keep Assembly Name
  • Knitr HTML Loop - Some HTML output, some R output
  • Upload files with Ajax and Jquery
  • Large data - storage and query
  • Do I've to free mysql result after storing it?
  • How can I estimate amount of memory left with calling System.gc()?
  • Rearranging Cells in UITableView Bug & Saving Changes
  • Warning: Can't call setState (or forceUpdate) on an unmounted component
  • WOWZA + RTMP + HTML5 Playback?
  • R: gsub and capture
  • Circular dependency while pushing http interceptor
  • jqPlot EnhancedLegendRenderer plugin does not toggle series for Pie charts
  • Comma separated Values
  • Linker errors when using intrinsic function via function pointer
  • A cron job substitute?
  • json Serialization in asp
  • How to set the response of a form post action to a iframe source?
  • Hits per day in Google Big Query
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • Setting background image for body element in xhtml (for different monitors and resolutions)
  • Is it possible to post an object from jquery to bottle.py?
  • How to load view controller without button in storyboard?