Reputation: 237
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 ?
Upvotes: 1
Views: 84
Reputation: 4115
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);
Upvotes: 1
Reputation: 1950
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/
Upvotes: 2
Reputation: 5654
$.getJSON('jsonOutput.php', function(data) {
var filename = data.imagesrc;
});
Upvotes: 2
Reputation: 47986
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
.
Upvotes: 1