Api post problem

Hello.
I’m facing a problem while I’m trying to make a $.ajax post request.
I readed here this documentation : https://frappe.github.io/frappe/user/en/guides/integration/rest_api.
I’m doing exactually what is being explained in CRUD part but isn’t working.
I have one doctype : Comissao.
I have one button in my program enrollment.
In the Comissao Doctype, I have a student as Mandatory.
I’m trying to add the student that I have at the program enrollment with a ajax POST but I’m receiving a 400 BAD REQUEST.
The following code is the button :
frm.add_custom_button(__(‘Comissao’), function() {
console.log(frm);
$.ajax({
url : “Frappe Cloud”,
dataType: ‘text’,
type: ‘POST’,
contentType: ‘application/x-www-form-urlencoded; charset=utf-8’,
success: function(data){
console.log(data);
}, error: function(error){
console.log(error);
}
});
});
As you showed you guys in the call that we made at skype.
How can I handle with this?
Thank you.

@ManasSolanki

Missing request data

@saurabh6790 oh sorry, I removed it, but it still not working.
Now it’s like this :
frm.add_custom_button(__(‘Comissao’), function() {
console.log(frm);

		$.ajax({
			url : "https://mlintercambios.erpnext.com/api/resource/Comissao",
			dataType: 'text',
			type: 'POST',
			contentType: 'application/x-www-form-urlencoded; charset=utf-8',
			data : {
				"student" : frm.doc.student
			},success: function(data){
				console.log(data);
			}, error: function(error){
				console.log(error);
			}
		});
	});

example:

$.ajax({
	url : "http://data.erpnext.dev:8000/api/resource/Role",
	dataType: 'text',
	type: 'POST',
	contentType: 'application/json',
	data : JSON.stringify( {
            "role_name" : "test"
            }
    ),
    beforeSend: function(xhr){xhr.setRequestHeader('X-Frappe-CSRF-Token', '9887b876105dae2f563a0de99ac893e2524a7394c9a0098cadd5b47e');},
    success: function(data){
		console.log(data);
	}, error: function(error){
		console.log(error);
	}
});

$.ajax({
url : “Frappe Cloud”,
dataType: ‘text’,
type: ‘POST’,
contentType: ‘application/json’,
data : JSON.stringify( {
“student” : frm.doc.student
}
),
beforeSend: function(xhr){xhr.setRequestHeader(‘X-Frappe-CSRF-Token’, ‘9887b876105dae2f563a0de99ac893e2524a7394c9a0098cadd5b47e’);},
success: function(data){
console.log(data);
}, error: function(error){
console.log(error);
}
});

@saurabh6790 I’m still receiving 400 BAD request.
This token is always the same? if not, where can I generate it?
in the network I can see this error message : “[”{"message": "Invalid Request", "indicator": "red"}“]”

@saurabh6790
now it’s working.
I think you should put this token part in the documentation because isn’t explaining this there.
Thank you for helping
$.ajax({
url : “Frappe Cloud”,
dataType: ‘text’,
type: ‘POST’,
contentType: ‘application/json’,
data : JSON.stringify( {
“student” : frm.doc.student
}
),
beforeSend: function(xhr){
xhr.setRequestHeader(
‘X-Frappe-CSRF-Token’, frappe.csrf_token
);
},success: function(data){
console.log(data);
}, error: function(error){
console.log(error);
}
});

1 Like

Yeah! Noted will fix this soon

1 Like