如何将JSON输出检索到JavaScript变量中?

I have my jsonOutput.php page like this :

 $response['imgsrc'] =  $filename[1];
 echo json_encode($response);

It outputs a filename like {"imgsrc":"071112164139.jpg"}

Now my question is, how can I use jQuery in my index.php to get the filename into a variable using ajax ?

$.getJSON('jsonOutput.php', function(data) {
  var filename = data.imagesrc;
});

All you'll have to do is execute a jQuery ajax call -

var imgSrc = '';
$.ajax(PATH_TO_PHP_SCRIPT,{},function(response){
  imgSrc = response.imgsrc;
},'json');

Your imgsrc parameter will now be a JavaScript variable called imgSrc. Don't forget to specify that your dataType is a json.

Reference -

Try this:

$.get('<your php file>', {}, function(response) {
   var imagesrc = response.imgsrc;
   alert(response.imgsrc);
});

Have a read through http://api.jquery.com/jQuery.get/

You can use jQuery.parseJSON to parse json output into a javascript object.

Following code would work:

var json = json_encode(<?php echo $response ?>);
var obj = jQuery.parseJSON(json);
alert("file name is: " + obj.imgsrc);