Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 为什么Ajax响应会添加post details WP_Javascript_Jquery_Ajax_Wordpress - Fatal编程技术网

Javascript 为什么Ajax响应会添加post details WP

Javascript 为什么Ajax响应会添加post details WP,javascript,jquery,ajax,wordpress,Javascript,Jquery,Ajax,Wordpress,出于某种原因,下面的Ajax响应添加了post细节,而这不是预期的应用程序 我只想使用最后返回的post id。 此外,我还试图弄明白为什么我的Ajax返回的细节是文本类型,而不是我希望的JSON类型 以下是我的JS代码: createRate(){ var ourNewPost = { 'title': $(".new-rate-title").val(), 'content': $('.new-rate-body').val(), '

出于某种原因,下面的Ajax响应添加了post细节,而这不是预期的应用程序

我只想使用最后返回的post id。 此外,我还试图弄明白为什么我的Ajax返回的细节是文本类型,而不是我希望的JSON类型

以下是我的JS代码:

createRate(){
    var ourNewPost = {
        'title': $(".new-rate-title").val(),
        'content': $('.new-rate-body').val(),
        'rate': $('.custom-star:checked').val(),
        'productId': $('.submit-rate').data('id')
    }

    $.ajax({
        // adding nonce key to make wordpress know we are looged in and has the permision to do that method.
        beforeSend: (xhr) => {
            xhr.setRequestHeader('X-WP-Nonce', ylsData.nonce);
        },
        url: ylsData.root_url + '/wp-json/yls/v1/rating/',
        type: 'POST',
        data: ourNewPost,
        dataType:"text",
        success: (response) => {

            console.log('congrats');
            console.log(response);
        },
        error: (response) => {
            console.log('failed');
            console.log(response);

        }
    }); 
}
下面是我的php路由代码:

add_action('rest_api_init','ylsRateRoute');


function ylsRateRoute(){
    register_rest_route('yls/v1','rating',array(
        'methods' => 'POST',
        'callback'=> 'createLike'
    ));

}

function createLike($data){
    if(is_user_logged_in()){
        $title = sanitize_text_field($data['title']);
        $content = sanitize_text_field($data['content']);
        $rateNum = sanitize_text_field($data['rate']);
        $productId = sanitize_text_field($data['productId']);
        if(get_post_type($productId) == 'product'){
            return wp_insert_post(array(
                'post_title' => $title,
                'post_type' => 'rate',
                'post_content' => $content,
                'post_status' => 'publish',
                'meta_input' => array(
                    'rate_related_product' => $productId,
                    'rating_rate' => $rateNum
                )
              ));
        }else{
            die("Invalid product id");
        }
      }else{
        die("you must be logged in first");
      }
}

因此,要回答您关于“为什么我的Ajax以文本类型而不是JSON形式返回详细信息”的问题`

这是因为您要求返回一个数组(而不是JSON)


如果需要JSON格式的响应,请使用JSON_encode()!,问题是我的自定义帖子类型的名称“rate”。。可能我的一个插件使用了相同的名称,因此它会影响ajax的结果。

事实上你是对的,我没有要求返回json,无论如何,这个wp\u insert\u post方法返回的id是Int。我仍然想知道如何删除post详细信息,并只返回我要求返回的id。
return wp_insert_post(array(...