'How we can call python POST request API from JQuery Ajax It showing CORS policy error
I have python API which is POST request,
http://192.168.2.25:8059/calculation
In Postman it return response like (Expected output should be same as like below)
[
{
"Indexs_q1": [ 13,14,15,],
"Points_q1": [ 69,72,75,]
}
]
In my case I am calling this API which is already in json format like ,
api_Object = {
"avg":[30,33,36],
"data":[10,20,30],
"Avg_xbar":66,
"ucl":116.776,
"lcl":15.224
}
$.ajax({
url: "http://192.168.2.25:8059/calculation",
type: "POST",
data: api_Object,
//data: JSON.stringify(api_Object),
dataType: "json",
success: function (response)
{
var resp = JSON.parse(response);
alert(resp.status);
},
error: function (xhr, status) {
alert("error");
}
});
NOTE : It showing error - Access to XMLHttpRequest at 'http://192.168.2.25:8059/calculation' from origin 'http://localhost:65047' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
Solution 1:[1]
After facing lots of issue finally I got an solution,
$.ajax({
url: "http://192.168.2.25:8059/calculation",
type: 'POST',
dataType: 'json',
cors: true,
contentType: 'application/json;charset=UTF-8',
secure: true,
data: api_Object,
headers: {
'Access-Control-Allow-Origin': '*',
},
success: function (data, status, xhr) {
console.log('success');
},
error: function (jqXhr, textStatus, errorMessage) {
console.log('error');
}
})
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Vinit Raypure |