jQuery ajax request with json response, how to?

后端 未结 5 970
一向
一向 2020-11-27 19:08

I am sending an ajax request with two post values, the first is "action" which defines what actions my php script has to parse, the other is "id" which i

5条回答
  •  我在风中等你
    2020-11-27 19:17

    Since you are creating a markup as a string you don't have convert it into json. Just send it as it is combining all the array elements using implode method. Try this.

    PHP change

    $response = array();
    $response[] = "link";
    $response[] = 1;
    echo implode("", $response);//<-----Combine array items into single string
    

    JS (Change the dataType from json to html or just don't set it jQuery will figure it out)

    $.ajax({
       type: "POST", 
       dataType: "html", 
       url: "main.php", 
       data: "action=loadall&id=" + id,
       success: function(response){
          $('#main').html(response);
       }
    });
    

提交回复
热议问题