Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
178 views
in Technique[技术] by (71.8m points)

javascript - How doo I resolve getting a 500 error when calling $.post

My website has a drawing tool. This drawing tool help met create an image (.png) from the drawing. Has been working for years now, but recently changed the wordpress theme.

Now I can't figure out what's going wrong.

I have the following code which is doing a POST request to a file in my theme folder

var templateUrl = 'https://example.nl/wp-content/themes/generatepress';
  var strDataURI = canvas.toDataURL("image/jpg",'',1.0);
  var jsnDataJSON = encodeURIComponent(JSON.stringify(canvas));
  $('input[name=json]').val(jsnDataJSON);
  strDataURI = strDataURI.substr(22, strDataURI.length);

  var ajax_urll= templateUrl+"/ajax.php";   
  
        $.post(ajax_urll,
        { 
            str: strDataURI
        },
        function(data){
            var obj=jQuery.parseJSON(data);
            if(obj.status!="ERROR"){
            jQuery("#drawing").html(obj.image);
            jQuery("#drawingsaveresult").html("Drawing has been successfully saved");
            jQuery("#drawingsaveresult").fadeOut(10000);
            jQuery("input[name='drawing']").val(obj.imagename);
            }else{
                jQuery("#drawingsaveresult").html("Else error in saving drawing");
                jQuery("#drawingsaveresult").fadeOut(10000);
                }
        });

So this code is calling ajax.php file (file in theme folder) which does the following

<?php
session_start();

// require_once( $_SERVER['DOCUMENT_ROOT'] . '/example.nl/wp-load.php' ); localhost
require_once( $_SERVER['DOCUMENT_ROOT'] . '/wp-load.php' );

global $wpdb;

$data = base64_decode($_POST["str"]);

$urlUploadImages = "oefeningen/";

$nameImage = rand()."drawing.png";

$img = imagecreatefromstring($data);

imageAlphaBlending($img, true);

imageSaveAlpha($img, true);
$response=array();
if($img) {
    imagepng($img, $urlUploadImages.$nameImage, 0);
    imagedestroy($img);
    get_stylesheet_directory_uri(); 
    $response['image']="<img src='".get_stylesheet_directory_uri()."/oefeningen/".$nameImage."' width=100 height=150>";
    $response['imagename']=$nameImage;
    $response['status']="success";
    
}
else {
    $response['status']="ERROR";
}


echo json_encode($response);

But for some reason it's not working anymore. In the console it's giving the following error back POST https://example.nl/wp-content/themes/generatepress/ajax.php 500

I can't find a resolution. Can someone assist me with this?

question from:https://stackoverflow.com/questions/65885840/how-doo-i-resolve-getting-a-500-error-when-calling-post

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...