jquery - get content back to javascript from PHP file in JSON format


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:



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


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; 

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


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?




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){

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

People are also looking for solutions to the problem: php - Calling different POST variables using Ajax


Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.