0

I have a very simple script like so:

$(function() {

    var url = theme_directory + '/func/api.php';

    $.get( url, function(data) {
       alert("Data Loaded: " + data);
     });

});

And the api.php looks like this:

<?php
echo('hello');
define('WP_USE_THEMES', false);
require_once( dirname( __FILE__ ) . '/../../../../wp-blog-header.php' );

This works. However when I put the exit below like so:

<?php
define('WP_USE_THEMES', false);
require_once( dirname( __FILE__ ) . '/../../../../wp-blog-header.php' );
echo('hello');

And I browse to the api.php it shows me 'hello' however when I use the javascript it doesn't show me the alert. Moving the hello back up, it works again. It gets me so confusedddd.


<?php
echo ' ';
define('WP_USE_THEMES', false);
require_once( dirname( __FILE__ ) . '/../../../../wp-blog-header.php' );
print_r($wp);

Now javascript will show me the contents of $wp... however, removing the echo ' '; and the javascript alert will not be triggered.


It has something to do with the WordPress Permalinks. When I turn it off, it works, when I turn it on it does not.

Mark
  • 16,906
  • 20
  • 84
  • 117
  • Have you looked at the response in Fiddler or in the browser's console to see what is being returned? – epascarello Feb 25 '12 at 14:26
  • There is no MAC version of Fiddler (don't say MAC is the problem ;)) – Mark Feb 25 '12 at 14:28
  • Than use [Charles](http://www.charlesproxy.com/) or the debugger in the browser. :) You can also set up [ajaxError()](http://api.jquery.com/ajaxError/) to catch the failed request. – epascarello Feb 25 '12 at 14:34
  • When I have echo on top it finds it, when I remove the echo on top Charles says it returns 404 Not Found =/ – Mark Feb 25 '12 at 14:35
  • but the requested url does exist when I visit it in the browser – Mark Feb 25 '12 at 14:36
  • When I turned off Permalinks of WordPress it does work, so I am guessing that has something to do with it. – Mark Feb 25 '12 at 14:42
  • What does `alert(url)` give you? An absolute or relative url? And what is the url of the page where you run this script? – Salman A Feb 25 '12 at 14:53
  • url gives me an absolute url to the API. I run it from the index page of the WordPress blog. Remember the script works if there is an output before the require_once. So it can all work. Also it all works if I don't enable permalinks in WordPress. – Mark Feb 25 '12 at 15:01

2 Answers2

1

I believe that WordPress's "fancy" permalinks use Apache's mod_rewrite. If Apache is returning an HTTP 301 to redirect the browser, jQuery won't follow that redirect. You might want to check out this Stack Overflow question to get around that.

Community
  • 1
  • 1
Brandan
  • 14,735
  • 3
  • 56
  • 71
  • Hmm interesting, would that mean that by echo-ing the redirect process won't occur and that's why it works? – Mark Feb 25 '12 at 15:10
  • That's what I'm guessing. Once you've echo-ed non-header content, Apache can't send anymore headers, so that prevents the redirect. – Brandan Feb 25 '12 at 15:36
0

the problem maybe is in require_once( dirname( __FILE__ ) . '/../../../../wp-blog-header.php' ); , that path is not found

however if you simply want to see the resulting string use load().

$('#input').load('api.php');
Mouna Cheikhna
  • 38,870
  • 10
  • 48
  • 69
  • Load will behave the same as the get above. The echo on top will make it work, put no echo above and it will not be triggered. that is the confusing part. – Mark Feb 25 '12 at 14:37