0

Having a spot of problem with a very simple example using PHP and jQuery.

PHP File

<?php
// This is where we return all of our initial widget markup
header('Content-Type: application/json');
$markup = array('markup' => 'No markup here');

echo json_encode($markup);

JS Function

    jQuery(document).ready(function($) {
        var widgetURL = "http://samedomain.com/vip/php/markup.php?callback=?";
        $.getJSON(widgetURL, {
            format: "json"
        }).done(function( data ) {
            console.log('Success!')
        }).fail(function (data) {
           console.log('Failed!');
        });
    });

However every time it fails. Even though checking the JSON shows it to be valid in the response. e.g. The headers say 200OK and list my valid JSON.

Valid JSON Returned with 200 OK response {"markup":"No markup here"}

Why then does the function fail?

webkenny
  • 221
  • 1
  • 7
  • 1
    any errors in your console? – wirey00 Mar 17 '14 at 22:57
  • your fail function will be passed 3 arguments `jqXHR, textStatus, errorThrown`, log the `textStatus` and/or `errorThrown` to see why it failed – Patrick Evans Mar 17 '14 at 23:01
  • Might be helpful http://stackoverflow.com/questions/5492838/why-does-getjson-silently-fail – sybear Mar 17 '14 at 23:04
  • I did log the errors thank you. It does say parse error in the errors but as you can see from the JSON above, it is indeed valid (I even validated it on JSONLint to be absolutely sure). The exact text of the error is: SyntaxError message: "Unexpected token :" stack: (...) get stack: function () { [native code] } set stack: function () { [native code] } __proto__: Error – webkenny Mar 18 '14 at 01:09

1 Answers1

0

Looks like it was a cause of the same domain! Finicky Javascript. ;)

Here's the total explanation. Thanks to all for your help.

See getJSON fails, JSON validates

Community
  • 1
  • 1
webkenny
  • 221
  • 1
  • 7