Reputation: 11
I want to Insert my signaturepad. I have taken the reference from https://codepen.io/BigLeeBrink/pen/ZdvLLE
Below is my controller code:
public function actionSignaturesave()
{
if (Yii::$app->request->post('case_id') != '') {
$case_id = Yii::$app->request->post('case_id', '');
$signatureData = Yii::$app->request->post('signature_image');
if($signatureData != "")
{
$signatureData = 'data:image/png;base64,'.$signatureData;
$filePath = Yii::$app->params['uploadpath'].'casesignature'.'/'. $model->case_id.'.png';
file_put_contents($filePath, file_get_contents($signatureData));
$urlatt = Yii::$app->urlManagerApi->createAbsoluteUrl('common/addmedia');
$newthumb = 0;
$tempname = $filePath;
$filetype = $this->commonModel->getFileTypeWithExtension($tempname);
$allowExtenssion = ["jpg","jpeg","png","gif","JPG","JPEG","PNG","GIF"];
if (in_array($filetype['extension'], $allowExtenssion) || count($allowExtenssion) == 0) {
$paramsatt = array(
"unitId" => @Yii::$app->session->get('user.unitId'),
"Title" => 'Signature Attachment Image',
"extension" => $filetype['extension'],
"ref_table" => 118,
"ref_id" => $model->case_id,
"file_type" => $filetype['type'],
"link" => "",
"isthumb" => $newthumb,
"is_external_link" => 0,
);
$resp = $this->commonModel->callApi($urlatt, $paramsatt, $tempname);
}
}
return $this->redirect('casedetail');
} else {
echo $this->functionModel->_jsonencode(['Status' => 500, 'Message' => \Yii::t('frontend', "something_went_wrong")]);
}
}
Here i have taken a one hidden input when i clicks on submit it's shows me my basecode64 url in value of hidden input type. Model Code:- Url::to(['defect/inspectionadd']), 'options' => ['id' => 'defect-inspection-add-form']]); ?>
$('#save-signature').on('click', function(e){
e.preventDefault();
var Sigimage=signaturePad.toDataURL();
// console.log(Sigimage);
document.getElementsByName('signature_image')[0].setAttribute('value', Sigimage);
signaturePad.clear();
});
$('#clear-signature').on('click', function(e){
e.preventDefault();
signaturePad.clear();
});
});
$(document).on('click', '#save-signature', function(){
var params = new Object();
params.case_id = '" . $model->case_id . "';
params.image = document.getElementsByName('signature_image').value;
$.ajax({
type : 'POST',
url : '" . Yii::$app->urlManager->createUrl('case/signaturesave') . "',
data : params,
success : function(data) {
data = JSON.parse(data);
if(data.Status != 200)
{
iplus.alert(data.Message);
return false;
}
}
});
return false;
});
");
Upvotes: 0
Views: 93
Reputation: 6169
This part is what's causing the server to respond with 302 Found
return $this->redirect('casedetail');
The redirect
method sets response status to 302
and sends the url the browser should be redirected to in Location
header.
From your javascript I would assume that you want to send JSON object with Status
and Message
properties. The proper way to do it in Yii 2 would be like this:
$response = Yii::$app->response;
$response->format = \yii\web\Response::FORMAT_JSON;
$response->data = [
'Status' => 200,
'Message' => 'some success message',
];
return $response;
This will cause server to respond properly with something like this:
HTTP/1.1 200 OK
Content-Type: application/json
... some other headers ...
{"Status":200,"Message":"some success message"}
You should also replace this error status response generation with proper way described above.
echo $this->functionModel->_jsonencode(...)
If you want you can set the proper status code for response with:
$response->statusCode = 500;
If your response properly declare application/json
as content type, you don't need to call JSON.parse()
in your success
callback because the json is already parsed for you when the data are passed to success callback.
Upvotes: 0