0

I know that you can get with getJSON the JSON array from PHP. But I cannot make it return the array so I can save it.

Is there a way to do this anyway?

For example if I made a function called getJSONInfo() and I want it to return the JSON array from PHP.

Example from answer of edwardmp:

$.getJSON("http://site.com/file.php?id=2", function(json){
    var newArray = json;
});

var username = newArray.username;
6
  • 1
    Saving it somewhere from the callback function of $.getJSON() should work if I understand your question correctly. Commented Jun 7, 2013 at 17:26
  • 1
    getJSON takes a callback function on success Commented Jun 7, 2013 at 17:26
  • 1
    Show us the code you have so far. Commented Jun 7, 2013 at 17:27
  • Oh so not like AJAX? I could just say var newArr = getJSONInfo() and use that variable anywhere I like for example? Commented Jun 7, 2013 at 17:27
  • don't forget to use json_encode before sending it from php, too Commented Jun 7, 2013 at 17:27

2 Answers 2

1

It would appear you want this code to work asyncrhonously:

var newArray = $.ajax({
    async=true,
    dataType: "json",
    url: "http://site.com/file.php?id=2"
});

// this line will only be reached AFTER the HTTP request has completed
alert(newArray.username);

However (alternatively) I would really recommend handling all of the code that uses myArray.username in the callback anyway. That's just how AJAX is meant to work in the end, otherwise it's just JAX! That would look somewhat like this:

function haz_json(newArray) {
    alert(newArray.username);
}
$.ajax({
    dataType: "json",
    url: "http://site.com/file.php?id=2",
    success: haz_json
});
Sign up to request clarification or add additional context in comments.

1 Comment

Yes this is how I solved it thank you. I was trying to avoid this because I heard its bad practice. But I think there is not other choice :S
1

Sure there is a way:

In your JS/HTML page:

$.getJSON("http://site.com/file.php", function(json){
    alert("JSON Data: " + json);
});

In your PHP file:

$results = array("key" => "value");
echo $_GET['callback'] . '(' . json_encode($results) . ')';

If you supply a GET parameter, use it like this, not directly in the url.

var myparameter = 'parameter content';

$.getJSON('site.com/file.php', { myparameter: escape(myparameter) }, function(data) {
    // code here
});

8 Comments

Well what I meant is to get teh array with getJSON. And use the array outside of the getJSON function.
The array is now in the json variable. Store in inside of another variable and you can use it outside of the getJSON function.
Okay thank you. So if I were to put that function you made in for example function getInformation(){"your function"}. Then I could say var newArray = getInformation(); And I would be able to use it like newArray.username to get the values right? Sorry just making sure.
Not newArray = getInformation(); but newArray = json; and place that instead of the alert line.
Could you see the example I editted in the answer and tell me if it would work that way?
|