First try simple request to /api/method/version
. or authenticated request to /api/method/ping
If simple requests work, there must be some other error.
First try simple request to /api/method/version
. or authenticated request to /api/method/ping
If simple requests work, there must be some other error.
I have tried your suggestion
and got an output of
Object { message: "9.2.22" }
but when i try the login request and used the same code above i got an Cross-Origin Request Blocked.
hmmm
so i tried to remove a line of code in my ajax request.
contentType: 'application/json; charset=utf-8',
and it works!
Thanks for your help @revant_one!
Running on production server
sudo nginx -t -c /etc/nginx/conf.d/frappe-bench.conf
got
nginx: [emerg] "upstream" directive is not allowed here in /etc/nginx/conf.d/frappe-bench.conf:1
nginx: configuration file /etc/nginx/conf.d/frappe-bench.conf test failed
Before even updating for cors.
By any change someone knows what is goiing on ?
you need to run nginx -t -c on /etc/nginx/nginx.conf
bench creates its own conf file that gets included in nginx.conf
Hi @revant_one
Thanks
I donāt know if this is fixed or not but you need to include headers
in ajax call.
This is my AngularJS code:
service.getUsers = function (formData) {
return $http({
method: 'POST',
url: urlBase + 'ec_site_survey.public.submit',
headers: {
'Content-Type': 'application/json',
'X-Frappe-CSRF-Token': '{{ frappe.session.csrf_token }}'
},
data: {
formData: formData
}
});
};
It worked for me.
No, I did not have to.
Just a question. Are you trying to call the api from another domain, or form the same?
If it from the same, use the one that I provided.
It is from another domain, that is why I have the CORS problem
modify /home/frappe/frappe-bench/config/nginx.conf
Good Day
Maybe this not same issue, but i need help in correct direction to query api from javascript in correct manner. Want to use some information of ERPNext on my website.
I can get values easily with postman, but struggle to get same with ajax:
function firstLogin(email,password) {
var url = āhttp://192.168.xxx.xxx/api/method/login?usr=user&pwd=Passwordā
$.ajax({
url: url,
headers: {āAcceptā: āapplication/jsonā},
contentType: āapplication/jsonā,
method: āPOSTā,
dataType: ājsonā,
crossDomain: true,
success: function(data){
alert(data);
}
}).fail(function(r){
alert(āFailed!ā);
});
}
Please help me.
Thank You
Thanks for replying, @revant_one.
Do you know what headers should I use?
I use
headers: {āAuthorizationā: āAPIKEYā, āContent-Typeā: āapplication/x-www-form-urlencodedā},
But I get this error:
Access to XMLHttpRequest at āhttp://165.227.109.214/api/method/versionā from origin āhttp://127.0.0.1:8080ā has been blocked by CORS policy: Request header field x-frappe-csrf-token is not allowed by Access-Control-Allow-Headers in preflight response.
Thanks
Hereās what I do usually.
http://165.227.109.214/api/method/version
1
.Hi @Albertus.
Take a look at Token Based Rest Api in JavaScript - #6 by CostaRica
Youāll find a simple but complete JavaScript and Python snippet for calling the ERPNext Rest Api
Hope this helps
Just a quick note for anyone struggling with CORS: @revant_oneās modifications to nginx.conf work great, but at least in some circumstances they apply the necessary headers only to successful API calls. That means, if the call fails for some other reason (403, 404, 500, etc.), you might get a misleading Access-Control-Allow-Origin
error.
In other words, if youāre still getting Access-Control-Allow-Origin
errors even after youāve changed frappe-bench/config/nginx.conf
and reloaded nginx, itās very possible that CORS isnāt actually the problem but being reported as a false positive. Adding this in hopes of saving someone else some trouble.
Signed,
Someone who learned the hard way
not able to understand what you are saying
pls can u explain in deep
How do we get actual status for errors like 401, 403 & 500 instead of CORS error?
I did not get cors error when the request returns with status code as 200. I have made the necessary configuration in nginx configuration also.
Excellent question, but unfortunately I donāt know the answer. My understanding of CORS and nginx configs is pretty superficial. Perhaps others in this thread might know, though.