douduandiao1368 2015-07-20 02:22
浏览 56
已采纳

让AJAX与Wordpress一起使用,不断返回0的响应

Hey i am using canvas to put an image out and save it. I can not get wordpress to make an ajax request. I keep getting back 0 responseText. I have beeen working on this for days and can't seem to get it to work. My page is http://studio-42.com/games.

jQuery( document ).on( 'click', '.draw-game', function() {

var testCanvas = document.getElementById("mycanvas");
var canvasData = testCanvas.toDataURL("image/png");
var postData = "canvasData="+canvasData;
var debugConsole= document.getElementById("debugConsole");
debugConsole.value=canvasData;

jQuery.ajax({
url : drawing.ajax_url,
type : 'post',
data : {
action : 'save_the_drawing',
imgBase64: postData,

},
success : function( response ) {
alert(response);

Here is my JS

And this one is the PHP code

add_action( 'wp_enqueue_scripts', 'drawing_game_enqueue_scripts' );
function drawing_game_enqueue_scripts() {
if( is_page('games') ) {
wp_enqueue_style( 'drawing-game', plugins_url( '/drawing-game.css', __FILE__ ) );
}
wp_enqueue_script( 'drawing-game', plugins_url( '/drawing-game.js', __FILE__ ), array('jquery'), '1.0', true );
wp_localize_script( 'drawing-game', 'drawing', array(
'ajax_url' => admin_url( 'admin-ajax.php' )
));
}

add_action( 'wp_ajax_nopriv_save_the_drawing', 'save_the_drawing' );
add_action( 'wp_ajax_save_the_drawing', 'save_the_drawing' );
function save_the_drawing() {



$img = $_POST['postData'];
$img = str_replace('data:image/png;base64,', '', $img);
$img = str_replace(' ', '+', $img);
$fileData = base64_decode($img);
//saving
$fileName = 'photo.png';
file_put_contents($fileName, $fileData);

}

I keep only getting back a 0 and its driving me crazy! Please Help. Thanks!

  • 写回答

1条回答 默认 最新

  • dsjswclzh40259075 2015-07-20 05:51
    关注

    You need to call die() at the end of your script. Simply returning or echoing will not suffice.

    WordPress by default returns 0 as a return code (0 being no errors detected) if you do not specify a return value (by calling die())

    Here's some additional details, and there's more in the WP Codex: https://wordpress.org/support/topic/ajax-call-returns-0

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 聚类分析或者python进行数据分析
  • ¥15 如何用visual studio code实现html页面
  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?