airesvsg / acf-to-rest-api

Exposes Advanced Custom Fields Endpoints in the WordPress REST API
https://wordpress.org/plugins/acf-to-rest-api/
1.33k stars 111 forks source link

Rest Forbidden 401 - 500 (Internal Server Error) #267

Open alexjuniodev opened 6 years ago

alexjuniodev commented 6 years ago

WP version: 4.9.8 ACF version: 5.7.2

Could someone, please, help me? I've tried for days update custom fields but I'm getting the following error:

Console Log:

jquery.js:9600 POST http://vendedor-confiavel.local/wp-json/acf/v3/vendedor/128 500 (Internal Server Error) send @ jquery.js:9600 ajax @ jquery.js:9206 updateProfile @ LoginScreenForm.js:51 dispatch @ jquery.js:5183 elemData.handle @ jquery.js:4991

INFO:

  1. I've also installed the Basic Auth plugin.
  2. I'am using this

beforeSend: (xhr) => { xhr.setRequestHeader('X-WP-Nonce', vcData.nonce); },

To auth

  1. Also tried ACF version 4.4.12

If I replace the url with http://vendedor-confiavel.local/wp-json/wp/v2/vendedor/128 only the title is updated, not the field "whatsapp".

Console Log:

http://vendedor-confiavel.local/wp-json/wp/v2/vendedor/128 LoginScreenForm.js:61 {id: 128, date: "2018-08-24T01:28:09", date_gmt: "2018-08-24T01:28:09", guid: {…}, modified: "2018-08-28T15:32:15", …}acf: facebook: nullwhatsapp: nullproto: Objectcontent: {raw: "", rendered: "", protected: false}date: "2018-08-24T01:28:09"date_gmt: "2018-08-24T01:28:09"guid: {rendered: "http://vendedor-confiavel.local/vendedor/128/", raw: "http://vendedor-confiavel.local/vendedor/128/"}id: 128link: "http://vendedor-confiavel.local/vendedor/128/"modified: "2018-08-28T15:32:15"modified_gmt: "2018-08-28T15:32:15"password: ""slug: "128"status: "publish"template: ""title: {raw: "Alex Silva", rendered: "Alex Silva"}type: "vendedor"_links: {self: Array(1), collection: Array(1), about: Array(1), wp:attachment: Array(1), wp:action-publish: Array(1), …}proto: Object LoginScreenForm.js:62 Updated

Code:

my.js

import $ from 'jquery';

class LoginScreenForm{
    constructor(){
        this.events();
    }

    events(){
        $(".save-button").on("click", this.updateProfile);
    }

    //test
    saveButton(){
        console.log('Im clicked');
    }

    updateProfile(e){
        var thisProfile = $(e.target);

        var data = {
            "title" : "Alex Silva 2",
            "acf" : {
                "facebook" : "@test",
                "whatsapp" : "12988998591"
            }
        };
        //vcData.root_url + '/wp-json/wp/v2/vendedor/' + thisProfile.data('id')
        //vcData.root_url + '/wp-json/acf/v3/post/' + thisProfile.data('id')
        var myURL = vcData.root_url + '/wp-json/acf/v2/vendedor/' + thisProfile.data('id');
        //var url  = WP_API_Settings.root + 'wp/v2/vendedor/' + thisProfile.data('id');
        console.log(myURL);

        $.ajax( {
            url: myURL,
            method: 'PUT',
            beforeSend: function( xhr ) {
                // https://developer.wordpress.org/rest-api/using-the-rest-api/authentication/
                xhr.setRequestHeader( 'X-WP-Nonce', WP_API_Settings.nonce );
            },
            data: data,
            dataType: 'json'
        } )
        .done( function ( data ) {
            alert( 'worked' );
        } )
        .fail( function( data ) {
            alert( data.responseJSON.message );
        } );;
    }
}

export default LoginScreenForm;

functions.php // Enable the option show in rest

add_filter( 'acf/rest_api/field_settings/show_in_rest', '__return_true' );

// Enable the option edit in rest
add_filter( 'acf/rest_api/field_settings/edit_in_rest', '__return_true' );

add_filter( 'acf/rest_api/item_permissions/get', function( $permission ) {
  return current_user_can( 'edit_posts' );
} );

add_filter( 'acf/location/rule_match/options_page', function(){
    return true;
} );

add_filter( 'acf/rest_api/option/prepare_item', function( $item ){
    if ( isset( $item['id'] ) ) {
        $item['id'] = 'options';
    }

    return $item;
});