i need to use frappe api call so to do that i have done as following
i have erpnext running at localhost:8003
i added following on nignx.conf file as in Access-Control-Allow-Origin Error for jquery $.ajax call
location @webserver {
if ($request_method = 'OPTIONS') {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'Authorization,DNT,X-Mx- ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Contro$
add_header 'Access-Control-Max-Age' 1728000;
add_header 'Content-Type' 'text/plain charset=UTF-8';
add_header 'Content-Length' 0;
return 204;
}
if ($request_method = 'POST') {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'Authorization,DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Contro$
}
if ($request_method = 'GET') {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'Authorization,DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Contro$
}
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Frappe-Site-Name thinksmart.site;
proxy_set_header Host $host;
proxy_set_header X-Use-X-Accel-Redirect True;
proxy_read_timeout 120;
proxy_redirect off;
proxy_pass http://frappe-bench-frappe;
}
then i added below code in my html file
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<script type="text/javascript">
var app = angular.module('postserviceApp', []);
app.controller('postserviceCtrl', function ($scope, $http) {
// $scope.name = null;
// $scope.age = null;
// $scope.adress = null;
$scope.lblMsg = null;
$scope.usr = null;
$scope.pwd = null;
$scope.postdata = function (username,password) {
$http.defaults.headers.post["Content-Type"] = "application/x-www-form-urlencoded";
var data = {
usr : 'Administrator',
pwd : '123'
// name: name,
// age: age,
// adress: adress
};
//Call the services
$http.post('http://localhost:8003/api/method/login', JSON.stringify(data)).then(function (response) {
if (response.data)
$scope.msg = "Post Data Submitted Successfully!";
}, function (response) {
$scope.responseData = response.data;
$scope.msg = "Service not Exists";
$scope.statusval = response.status;
$scope.statustext = response.statusText;
$scope.headers = response.headers();
});
// $http({
// url: "//httpbin.org/post",
// method: "POST",
// data: data
// }).success(function(data, status) {
// $scope.response = "POST Response: " + data.form.json;
// $scope.fullResponse = JSON.stringify(data);
// });
};
});
</script>
this js file is located at another folder of same pc so to make it run as localhost i use nodes.js
http-server ālocation of js folderā -o
but i get this
XMLHttpRequest cannot load http://localhost:8003/api/method/login. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:31338' is therefore not allowed access. The response had HTTP status code 417.
can any one help me this.
i tried a lot .