我的Wordpress插件有一些问题:
$.ajax(send_rendered_photo.ajaxurl+"?action=save_photo",{
type: "POST",
data:{imgURL: dataURL},
contentType: "application/json; charset=utf-8",
dataType: "json",
complete: function(response){
},
success: function(data) {
alert(data.data);
},
error: function(data){
alert("fail");
}
});
return false;
当我的js文件中有这个jQuery代码时,响应是null,在PHP中 控制台中有: POSThttp://localhost:81/wordpress/wp-admin/admin-ajax.php 400(错误请求( 这是我的PHP: 我不知道问题出在哪里。感谢的帮助 编辑:有标题和响应send_rendered_photo.jaxurl响应的照片 标题 这是完整的js代码 编辑编辑:我在js文件中更改";data:{imgURL:dataURL}"至";data:dataURL";和$.ajax到$.post,它工作$_POST['imgURL']
是null$.ajax(send_rendered_photo.ajaxurl,{
type: "POST",
data:{
imgURL: dataURL,
action: "save_photo",
},
contentType: "application/json; charset=utf-8",
dataType: "json",
complete: function(response){
},
success: function(data) {
alert(data.data);
},
error: function(data){
alert("fail");
}
});
return false;
function l_m_add_ajax_js(){
wp_enqueue_script('l_m_ajax_render_photo', plugins_url('scripts/save_photo.js', __FILE__), true);
if(isset($_SERVER['HTTPS'])){
$protocol = 'https://';
}
else{
$protocol = 'http://';
}
$params = array('ajaxurl'=>admin_url('admin-ajax.php',$protocol));
wp_localize_script('l_m_ajax_render_photo','send_rendered_photo', $params);
}
add_action('wp_footer', 'l_m_add_ajax_js');
add_action('wp_ajax_save_photo', 'save_photo');
add_action('wp_ajax_nopriv_save_photo', 'save_photo');
function save_photo(){
$imgURL = $_POST['imgURL'];
$response = array(
'success'=>true,
'data'=>$imgURL
);
print json_encode($response);
exit;
}
$("#save").on('click',function(){
html2canvas(document.getElementById("create")).then(function (canvas) {
var dataURL = canvas.toDataURL("image/png", 0.9);
alert(dataURL);//dataURL is ok
$.ajax(send_rendered_photo.ajaxurl+"?action=save_photo",{
type: "POST",
data: dataURL,
dataType: "json",
complete: function(response){
},
success: function(data) {
alert(data); // data is undefined
},
error: function(data){
alert("fail");
}
});
return false;
});
});
您确定send_rendered_photo.ajaxurl变量包含正确的URL并且不是空的吗?