如何进行图像的Ajax调用?

时间:2019-03-31 23:01:53

标签: php jquery ajax

我正在尝试进行ajax调用,当选择“ Vanilla”选项时会显示图像。到目前为止,能够成功拨打一个显示说明和价格的电话,至于图像我该怎么做?

        $("#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。

0 个答案:

没有答案