Send PHP array to script via Ajax using jQuery

RationalRabbit
RationalRabbit used Ask the Experts™
on
I have a PHP array
$D = array("Key1"=>"Value1","Key2"=>"Value2") etc.
I need to pass this to a PHP script using jQuery AJAX.
What is the best method to do this?
I know that json_encode should work, but not sure how to implement it in the data definition, which is also sending other variables
tried data: {Var1; "Value1", Var2: "Value2", DArray: <?PHP json_encode($D) ?>} (among other things)
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Multitechnician
Top Expert 2014
Commented:
json.php :

<?PHP
   header('Content-Type: application/json');
   $D = array("Key1"=>"Value1","Key2"=>"Value2")
   echo json_encode($D); // https://www.php.net/manual/fr/function.json-encode.php

Open in new window


page :
<html><head>
<script src="jquery.js"></script>
<script>
   jQuery.getJSON("json.php", function(response) { // https://api.jquery.com/jQuery.getJSON/
           alert( response.Key1 );
   });
</script> 
</head><body></body></html>

Open in new window

Author

Commented:
Thanks
leakim971Multitechnician
Top Expert 2014

Commented:
you welcome

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial