Я пытаюсь сделать ajax-вызов, который отображает изображение, когда выбрана опция «Ваниль». До сих пор был в состоянии успешно сделать звонок, который отображает описание и цену, как для изображения, как бы я это сделал?
$("#select").change(function() {
//alert("test");
$selectedChoice = $(this).val(); // Gets selected value
//alert($selectedChoice);
if($selectedChoice != "none"){
//alert("ifstatement");
if($selectedChoice == "vanilla") { //Gets data from JSON files
//alert("test");
$.ajax({
type:'GET',
dataType:'json',
url:'Vanilla.js',
success:function(returnObject){
//alert("cupCake")
//alert(returnObject);
//alert(returnObject.cupcake_desc);
//var cupcakeObject = JSON.parse(returnObject); A parse is not required since the ajax call will parse the object for you.
$("#description").html(returnObject.cupcake_desc);
$("#price").html(returnObject.cupcake_price);
alert(returnObject.cupcake_img);
$("#img").attr("src", "images/vin.jpg" + returnObject.cupcake_img);
}
Это мой PHP-файл:
$cupcakeObject->flavor = "Vanilla";
$cupcakeObject->cupcake_desc = "A vanilla cupcake";
$cupcakeObject->cupcake_price = "$5";
$cupcakeObject->cupcake_img = images/vin.jpg;
$myJSON = json_encode($cupcakeObject);
$my_file = $cupcakeObject->flavor . ".js";
$handle = fopen($my_file, 'w') or die('Cannot open file: '.$my_file);
$data = $myJSON;
fwrite($handle, $data);
Создается файл Vanilla.js, отображающий мое изображение кекса в виде NANjpg.