doulao3078
2014-09-13 16:43
浏览 62

为什么AJAX发布Wordpress不起作用?

I'm trying to posting post via ajax at Front End, but I'm always failed.

What I'm doing wrong? here is my code it returns me 0:

HTML

<form id="add" name="add" method="post" enctype="multipart/form-data">
    <label for="title">Title</label>
    <input id="title" type="text" name="post_title" value="">
    <input type="hidden" name="action" value="my_action">
    <input id="submit" value="Submit" type="submit">
</form>

PHP

wp_enqueue_script( 'my-ajax-request', '/js/my_js.js' );
wp_localize_script( 'my-ajax-request', 'MyAjax', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ), 'MyAjax.action' => 'my_action', 'MyAjax.post_title' => $_POST['post_title'] ) );

add_action('wp_ajax_nopriv_my_action', 'addpost_ajax_handler' );
add_action('wp_ajax_my_action', 'addpost_ajax_handler' );
function addpost_ajax_handler() {

echo 'reached ajax handler'; // delete this line later

if( 'POST' == $_SERVER['REQUEST_METHOD'] ) {
    $title = $_POST['post_title'];
    $my_post = array(
        'post_title' => $title
    );
    $result = wp_insert_post( $my_post );
    if ( ! is_wp_error( $result ) ) echo 'success';
}
die();
}

my_js.js

$('#submit').on('click', function(e) {
e.preventDefault();
var data = { 'action': MyAjax.action, 'post_title': MyAjax.post_title };
    $.post(MyAjax.ajaxurl, data, function(response) {
        alert(response);
    });
});

图片转代码服务由CSDN问答提供 功能建议

我正在尝试通过前端的ajax发布帖子,但我总是失败。

我做错了什么?\我的代码返回0 0:

HTML

   &lt; form id =“add”name =“add”method =“post”enctype =“multipart / form-data”&gt; 
&lt; label for =“title”&gt; Title&lt; / label&gt; 
&lt;  input id =“title”type =“text”name =“post_title”value =“”&gt; 
&lt; input type =“hidden”name =“action”value =“my_action”&gt; 
&lt; input id  =“submit”value =“Submit”type =“submit”&gt; 
&lt; / form&gt; 
   
 
 

PHP

  wp_enqueue_script('my-ajax-request','/ js / my_js.js'); 
wp_localize_script('my-ajax-request','MyAjax',array('ajaxurl'=&gt; admin_url('  admin-ajax.php'),'MyAjax.action'=&gt;'my_action','MyAjax.post_title'=&gt; $ _POST ['post_title'])); 
 
add_action('wp_ajax_nopriv_my_action','addpost_ajax_handler'  ); 
add_action('wp_ajax_my_action','addpost_ajax_handler'); 
func  ing addpost_ajax_handler(){
 
echo'达到了ajax handler';  //稍后删除此行
 
if('POST'== $ _SERVER ['REQUEST_METHOD']){
 $ title = $ _POST ['post_title']; 
 $ my_post = array(
'post_title'  =&gt; $ title 
); 
 $ result = wp_insert_post($ my_post); 
 if(!is_wp_error($ result))echo'success'; 
} 
die(); 
} 
 <  /code>

my_js.js

$('#submit').on('click',function(e){  
e.preventDefault(); 
var data = {'action':MyAjax.action,'post_title':MyAjax.post_title}; 
 $ .post(MyAjax.ajaxurl,data,function(response){
 alert( 响应); 
}); 
}); 
   
 
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • doucang6739 2014-09-14 17:25
    已采纳

    thanks to all, problem solved. I needed to put the code of php handler in functions.php, instead a custom page)

    已采纳该答案
    打赏 评论
  • douxiexie3574 2014-09-13 18:49

    To check if you actually receive data from the function in php, make some additions:

    function addpost_ajax_handler() {
    
        echo 'reached ajax handler'; // delete this line later
    
        if( 'POST' == $_SERVER['REQUEST_METHOD'] ) {
            $title = $_POST['post_title'];
            $my_post = array(
                'post_title' => $title
            );
            $result = wp_insert_post( $my_post );
            if ( ! is_wp_error( $result ) ) echo 'success';
        }
        die();
    }
    

    If you still get just 0, try ensuring you send the action parameter in a way it can be understood:

    data: { action: my_action, datastr: dataString },
    

    Another possible troublemaker could be the relative url, you should use a full url path to ensure your script runs regardless of the page you are on. For that, check http://codex.wordpress.org/AJAX_in_Plugins and see how they use wp_localize_script to send the correct admin url.

    打赏 评论
  • duanjupiao4730 2014-09-14 17:14

    You can first try with commenting the code of post insertion for a while and just echo the 'reached ajax handler', to verify first of all, the ajax request is successful and then you can move forward to add the post insertion code step by step.

    Apart from this, you can add an 'else' part e.g. else {echo 'failed';} just to verify if there is an issue while creating the new post and it returns 'failed' in response.

    打赏 评论

相关推荐 更多相似问题