Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Help With Post Data #14

Closed
alexjuniodev opened this issue Aug 28, 2018 · 3 comments
Closed

Help With Post Data #14

alexjuniodev opened this issue Aug 28, 2018 · 3 comments

Comments

@alexjuniodev
Copy link

alexjuniodev commented Aug 28, 2018

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

I've also installed the Basic Auth plugin.

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: null__proto__: 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

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

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

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

	var ourUpdatedPost = {
		"title" : "Alex Silva",
		"acf" : {
			"whatsapp" : "12988998591"
		}
	};
	var myUrl =  vcData.root_url + '/wp-json/acf/v3/vendedor/' + thisProfile.data('id');
	console.log(myUrl);
	$.ajax({
		beforeSend: (xhr) => {
			xhr.setRequestHeader('X-WP-Nonce', vcData.nonce);
		},
		url: myUrl 	,
		type: 'POST',
		data: ourUpdatedPost,
		success: (response) => {
			console.log(response);
			console.log("Updated");
		},
		error: (response) => {
			console.log(response);
			console.log("Error");
		}
	});

}

}

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;

});

@airesvsg
Copy link
Owner

airesvsg commented Aug 28, 2018

Hi @alexjuniodev,

Please, read more about authentication.

https://developer.wordpress.org/rest-api/using-the-rest-api/authentication/

Thanks

@alexjuniodev
Copy link
Author

@airesvsg I've already read this. Actually, I read all the issues on this project. I'm passing the nonce with

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

Do you have another guess? I'm struggling last days with it

@alexjuniodev
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants