dqk42179 2017-05-09 19:42
浏览 54

jQuery AJAX无法在WordPress安装中找到本地文件

I'm having trouble with a WordPress plugin I've been working on. A JS file is loaded as a resource with each page/post opened, which in turn has a request to load the contents of an HTML file.

Being that the page/post directories change frequently, I'm having a difficult time making the jQuery dynamically pin down the location of the file (even though it's in the same location as the rest of the plugin resources).

An example:

  jQuery('body').append('<section id="asub00LOAD"></section>');

var url = jQuery(location).attr('hostname');
var dir = url + '/wp-content/plugins/adsenseunblock/html/adunblock.html #asub00AJAX';

jQuery('#asub00LOAD').load(dir);

That was placing the whole URL path to the file after the local install ("root.com/CHEETOS/" in this case): enter image description here

After which, I did this, which works fine for the root directory only:

  jQuery('body').append('<section id="asub00LOAD"></section>');

var dir = 'wp-content/plugins/adsenseunblock/html/adunblock.html #asub00AJAX';

jQuery('#asub00LOAD').load(dir);

After you venture to another page, obviously the directory location is wrong again.

I tried to place some PHP into my JS file before so I could take advantage of the $plugins_url feature, but that became very convoluted and it's hard to track any errors without a PHP console to work from...

I hope someone here will have a solution for me!

  • 写回答

1条回答 默认 最新

  • douzuo0711 2017-05-09 20:10
    关注

    The first example probably fails because there's no http:// (or //)

    Use var url = "//" + location.hostname;

    The second example should work everywhere if you use a root-relative path:

    var dir = '/wp-content/plugins/adsenseunblock/html/adunblock.html #asub00AJAX';

    评论

报告相同问题?