0

When sending the request from the jQuery Mobile script to the specified PHP file, nothing is returned, nothing is appended to the html file. Here's the URL of the page:

localhost/basket/newstext.html?url=http://www.basket-planet.com/ru/news/9235

newstext.html:

<head>
<script src="js/newstext.js"></script> 
</head>
<body> 
<div data-role="page" id="newstext"> 
  <div data-role="content"> 
    <div id="textcontent"></div> 
    </div> 
  </div> 
</body> 

newstext.js:

var serviceURL = "http://localhost/basket/services/"; 
$('#newstext').bind('pageshow', function(event) { 
var url = getUrlVars()["url"]; 
$.getJSON(serviceURL + 'getnewstext.php?url='+url, displayNewsText); 
}); 

function displayNewsText(data){ 
  var newstext = data.item; 
  console.log(newstext);
  $('#textcontent').text(newstext); 
  $('#textcontent').trigger('create'); 
} 

function getUrlVars(){ 
//it displays in the alert perfectly, shortening the message here 
} 

getnewstext.php:

<?php 
include_once ('simple_html_dom.php'); 
$url = $_GET['url']; 
$html = file_get_html(''.$url.''); 

$article = $html->find('div[class=newsItem]'); 
$a = str_get_html(implode("\n", (array)$article)); 
//parse the article 
header("Content-type: application/json");
echo '{"item":'. json_encode($a) .'}';
?>

I think my problem is how I'm encoding the $a variable in the PHP script. The $a variable contains html tags of all kind...how can I append it in the html file?

3
  • make sure you are setting the content header before you echo stuff, header("Content-type: application/json"); it might be as simple as jquery/your browser not knowing what to do with the returned blob of text Commented Apr 4, 2013 at 21:54
  • also try doing a console.debug on data inside of your displayNewsText function to see the format of what is in that var. You will need to open the console for your browser (consult google) to see the output Commented Apr 4, 2013 at 21:55
  • I updated the text...can you please check if I updated it correctly? The response in the console is empty for newstext.html...on another page, where I get data from a mysql query, the response is the correct php file with data. Response for this page is empty... Commented Apr 4, 2013 at 22:19

1 Answer 1

1

Where you have this line:

$.getJSON(serviceURL + 'getnewstext.php?url='+url, displayNewsText);

Change it to be:

$.getJSON(serviceURL + 'getnewstext.php?url='+url, displayNewsText, function(response){
    $('#elem').append(response);
});

Where #elem is the name of the element that you want to append the data, returned from the PHP file, to.

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

5 Comments

Isn't that what I'm doing? The displayNewsText is the function that handles the response...so I should get rid of displayNewsText?
Oops, I didn't realise that was a function call; what you need to do is add in another parameter because the first getJSON parameter is the url, second is the data to be passed to the URL, and the 3rd is the function call which deals with the response. I think you just need to add in another parameter then.
Or, for better clarity, you may want to use the plain $.get method jQuery offers, that way explicitly set the success/error/complete callbacks. api.jquery.com/jQuery.get
Would that function work with .bing('pageshow'...)? I think pageshow is something with jQuery Mobile only, but I may be wrong. So the only function is getJSON
$.get()? Yeah, that'll work if you replace $.getJSON() with $.get(). $.getJSON is just a helper method which uses $.get(), but with $.get(), you have the .success(), .error(), and .complete() methods which will probably be clearer to see what you're doing with the AJAX response.

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.