开发者

How to parse the data obtained from $.get() function

开发者 https://www.devze.com 2023-02-08 02:37 出处:网络
I obtained the data from the server using ajax like this: (EDITED) $(document).ready(function() { setInterval(function()

I obtained the data from the server using ajax like this: (EDITED)

$(document).ready(function()
{
  setInterval(function()
  {
     $.开发者_StackOverflowget('/forms/requestProcessor.php', function(data) 
     {
        $("#shout").append(data.question+'<br>');
        alert('Load was performed.');
     },'JSON');
  }, 5000); // 5 seconds
});

In the php file,i send data like this:

while($row=mysql_fetch_array($result))
{
  $question=$row['Question'];
  $choice1=$row['Choice1']; 
  $choice2=$row['Choice2'];
  $choice3=$row['Choice3'];
  $choice4=$row['Choice4'];

  $return_data->question = $question;
  $return_data->choice1 = $choice1;
  $return_data->choice2 = $choice2;
  $return_data->choice3 = $choice3;
  $return_data->choice4 = $choice4;

  echo(json_encode($return_data));
}

It prints "undefined". However, if i point the browser directly to the php file, it shows the data properly in json format


Yes, it is possible most easiest way is to return json_encoded string. so your code would look like this php:

    header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");

    header("Last-Modified: " . gmdate("D, d M Y H:i:s") . "GMT");

    header("Cache-Control: no-cache, must-revalidate");

    header("Pragma: no-cache");

    header("Content-type: text/x-json");

    echo json_encode($array);

    exit(0);

and JavaScript would look like this:

 $.get('/forms/requestProcessor.php', function(data) 
 {
  ....
 }, 'json');


Sure. It'll be easier if you were to put it into, say, <p></p> pairs, but what you're doing with the data is putting it into DOM nodes. jQuery is more than happy to let you get the results out of those DOM nodes again.

But you could make life easier on yourself if you had the PHP return JSON, like

[ question, ans1, ans2, ans3, ans4]

or even better as

{ "question" : "my question text",
  "answers"  : [a1,a2,a3,a4]
}


it looks as though you are recieving a result string, in which there is no real structure.

if you want, you could use json_encode() in your encode whatever data you are wanting to send back in json form, and change the setting of your $.get() request to expect json, this way you could simply access them via data.choice2 etc...

example

$return_data->question = $question;
$return_data->choice1 = $choice1;
$return_data->choice2 = $choice2;
...
$return_data->choice5 = $choice5;

echo json_encode($return_data);

and then for your $.get()

$(document).ready(function()
{
  setInterval(function()
  {
     $.get('/forms/requestProcessor.php', function(data) 
     {
        $("#shout").append(data+'<br>');
        alert('Load was performed.');
     },'json');                //<-- added, last argument to expect 'json'
  }, 5000); // 5 seconds
});

now the data will be returned in json format, and will be parsed by jQuery because it expects it to be json.

So you could now access the data in your success function with statements like data.question, data.choice1, etc...


Its better if you encode the return values of "/forms/requestProcessor.php" into JSON format. Then read it from the client with jquery.parseJSON()

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


In php you can store response in an array and transform it using json_encode function. Before in jQuery you can retrieve json using $.parseJSON(data)


U have to use jQuery.parseJSON for this..

example:

"var obj = jQuery.parseJSON('{"name":"John"}');
alert( obj.name === "John" );"

click me to know briefly

0

精彩评论

暂无评论...
验证码 换一张
取 消