tim
tim

Reputation: 3296

Handling JSON repsonse

I'm doing an AJAX call using jQuery's JSON feature.

function get_words_json(address, username, paging_value) {
 $.ajax({
  type: "GET",
  url: "json/" + address,
  dataType: "json", 
  data: "username=" + username + "&paging_no_st=" + paging_value,
  success: function(json){
   pop_word_list(json, paging_value);
  }
 });
}

As you can see, I'm sending the response to another JavaScript function, but what I'd like to do is send the response to PHP. Is this possible, say to directly convert the response into a PHP array (not using JavaScript) and then use PHP to handle the array, etc?

Thanks in advance.

Upvotes: 0

Views: 254

Answers (3)

naugtur
naugtur

Reputation: 16915

The whole idea doesn't stick together at all... but:

  1. If there is a reason to do that - then You want to do the $.post('phpfile.php',json,function(){},'text or whatever type You want in return'); and the whole json object goes to PHP's $_POST[] as suggested above, but I can see NO case where it should be done that way.

  2. If You get that json from some code You can't change and want to use data in php do:

    • use cURL to get the data from another thing
    • use json_decode($data,true) to get assoc table of the whole thing
  3. If You don't know what You're doing :)

    • just pass the object to another function without useless sending stuff back and forth. You might want to do empty AJAX call to trigger the php file, nothing more.

Upvotes: 0

andres descalzo
andres descalzo

Reputation: 14967

do this?

js (ajax) -> php (array conver to ajax) -> js (ajax) -> php ?



function get_words_json(address, username, paging_value) {
 $.ajax({
  type: "GET",
  url: "json/" + address,
  dataType: "json", 
  data: "username=" + username + "&paging_no_st=" + paging_value,
  success: function(json){
   json["paging_value"] = paging_value;
   $.post("x.php", json);
  }
 });
}

Upvotes: 0

Jacob Relkin
Jacob Relkin

Reputation: 163248

You could perform another Ajax call to the php script in the success function, passing along the JSON data as a POST param.

Upvotes: 2

Related Questions