0

I'm trying to figure out how to retrieve simple html content using JSON in a jQuery Mobile.

Here is how I have done so far:

matchinfo.php:

$str = '<h2>This is a test</h2><p>What is going on here?</p>';

echo $_GET['callback'] . '(' .json_encode($str). ')';  

And here is how I try to recieve it:

$(document).on('pageshow', function (){

    $.mobile.loading('show');
    $.getJSON("http://mypage.com/matchinfo.php", function (result){
        $("#pageName").html(result.html);
    });

    $('#pageName').content('refresh');
    $.mobile.loading('hide');

});

The spinner just keeps loading! I am able to do this with listview, but here I just want to get some content!?

Any help is appreciated and thanks in advance :-)

3
  • Your code is looking for a key called html but your data is just a flat string. json_encode(array('html' => $str)); Commented Apr 16, 2013 at 15:17
  • shouldnt you have a ?callback=someCallback in the url ? Commented Apr 16, 2013 at 15:18
  • @DaveRandom - Just perfect... didn't see that :-) Thanks... Commented Apr 16, 2013 at 15:39

2 Answers 2

1

You're not outputting JSON, you're outputting JSONp. You probably shouldn't use getJSON for that, JSONp isn't supposed to work with XMLHTTPRequest (AJAX).

getJSON is shorthand for:

$.ajax({
  dataType: "json",
  url: url,
  data: data,
  success: success
});

http://api.jquery.com/jQuery.getJSON/

Sign up to request clarification or add additional context in comments.

Comments

1

You can use jQuery $.get instead of $.getJSON

$.get('http://mypage.com/matchinfo.php', function(result) {
    $('#pageName').html(result);
});

Also notice that in the .html() function you just need to pass result instead of result.html

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.