Пользовательская конечная точка REST API вызывает ответ об ошибке HTTP 500. Почему? [дубликат]Php

Кемеровские программисты php общаются здесь
Ответить
Гость
 Пользовательская конечная точка REST API вызывает ответ об ошибке HTTP 500. Почему? [дубликат]

Сообщение Гость »


I'm fairly new to WordPress development.

I am developing a theme and I have registered a new post type called 'notes' that users can register in site and see their own notes and create/edit/remove them via WordPress REST API.

I want to limit my users so that they couldn't have more than 4 notes.

For this purpose, I used a function in functions.php and hooked it into 'wp_insert_post_data' filter hook and I used in this function to send a custom error message to the JavaScript method callback.

However, the callback doesn't return this custom error message. Instead, it gives another error message :

"Internal Server Error" with "status: 500"

Thank you in advance for your guidance.

This is the code I'm using in functions.php:

Код: Выделить всё

function noteSettings($data) {     if($data['post_type']=== 'note') {         if(count_user_posts(get_the_user_id(), 'note') > 4) {             die('your notes count has reached to Limit; please delete a note');         }         $data['post_content']= sanitize_textarea_field($data['post_content']);         $data['post_title']= sanitize_text_field($data['post_title']);     }     if($data['post_type']=== 'note' AND $data['post_status'] !== 'trash') {         $data['post_status']= 'private';     }          return $data; } add_filter('wp_insert_post_data', 'noteSettings'); 
And this is my Javascript code:

Код: Выделить всё

    async createNote(event){         fetch(`${themeData.root_url}/wp-json/wp/v2/note`, {             method: 'POST',             headers : {                 'content-type': 'application/json',                 'X-WP-Nonce': themeData.nonce             },             body : JSON.stringify({                 'title': currentTitle.value,                 'content': currentBody.value,                 'status': 'private'             })         }         )             .then(res=>{                 res.json()                 console.log(res);             })             .then(data => this.makeNoteTemplate(currentTitle, currentBody, data.id))     } 
in fact I don't want to show the error message to users with php at all so enabling errors for php doesn't help me. instead I just want to send a custom error message for response to "POST HTTP REQUEST" that leads when i print Response of request in browser console, statusText's value will be equal to that custom error message instead of "Internal Server Error" and then I could decide to how show error to user.

My problem has solved and solution was that we need to wrap the error text in json_encode() php function to could send it for javascript! thank you all


Источник: https://stackoverflow.com/questions/780 ... sponse-why
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «Php»