Render page in return of a Ajax call

Hello community,

I have a Ajax call who render a page in a controller and return the result.
But I can’t figure out how to render that page in place of the current one from the Ajax call.

Ajax code:

$.ajax( { 
  type: "POST", 
  url: "' . $url . '",
    dataType: "text",
    data: {
      base64data : convertedImage,
    },
    success:function(result, status) {
      if(result) {
        alert("' . Yii::t('SLf', 'msg__technical_support_sent') . '");
        var response = JSON.parse(status);
        $("#body").html(status);
      } else {
        alert("' . Yii::t('SLf', 'msg__technical_support_sent_error') . ' (" + result + " - " + status + ")");
      }
    },
    error:function(requestObject, error, errorThrown) {
      alert("' . Yii::t('SLf', 'msg__technical_support_sent_error') . ' (" + requestObject + ", " + error + ": " + errorThrown + ")");
    }
});

Yii::Controller return call:

  echo json_encode($this->renderAjax('contact', ['model' => $model]));
  return true;

Thank you.

in your controller all you need is to renderAjax:

return $this->renderAjax('contact', ['model' => $model]);

Then in the calling JS you need to populate the html to the container where it’s supposed to be after ajax success:

$("#body").html('content from ajax response here');

If you are returning HTML markup from your ajax call, you can also set “dataType” to “html”. No need to json encode it.
As emuthomi said, in the controller just return your html code.
In your success callback, just use the “result” variable.

I don’t think replacing all the content is the is a good practice.
You may end up removing some script tags generated by the framework near the closing tag.
Unless it’s a really simple results page or something like that.
Create a div inside the , give it some id and put the contents in it.

Thank you both,

But I need to replace all content of the page since it’s a new page.

The Ajax takes a screen shot, send it to the server and the server return to a contact form with the file ready to send after the user completed the rest of the form.

So I just called window.location.href = “/index.php?r=site%2Fcontact” after Ajax return.
Easier and faster.