I am creating a web page that call remote data(json). for that i used jQuery.ajax it is good when i am calling the page in same domain. But if I call this from another domain (like: localhost) browser is blocking by saying
No 'Access-Control-Allow-Origin' header is present on the requested resource
but If I use dataType: 'JSONP'
with ajax then browser is not blocking but getting this following error though it is a valid json object:
Uncaught SyntaxError: Unexpected token :
at p (jquery.min.js:2)
at Function.globalEval (jquery.min.js:2)
at text script (jquery.min.js:4)
at Nb (jquery.min.js:4)
at A (jquery.min.js:4)
at XMLHttpRequest.<anonymous> (jquery.min.js:4)
This is my ajax code:
$(function () {
$.ajax({
url: "/GarmentTech/api/get_products.php",
type: "GET",
success: function (result) {
$('.text').text('');
console.log(result);
console.log(result);
for (var i = 0; i < result.products.length; i++) {
var place = `
<tr>
<td>${result.products[i].name}</td>
<td>${result.products[i].description}</td>
<!--<td>${result.products[i].type}</td>-->
<td>${result.products[i].model_color}</td>
<td>${result.products[i].size}</td>
<!--<td>${result.products[i].manufacturer}</td>-->
<td>${result.products[i].purchase_rate}</td>
<td>${result.products[i].sales_rate}</td>
<td style="text-align:right;">
${result.products[i].stock_count}
${result.products[i].unit_type}
</td>
</tr>
`;
$('.product_view').append(place);
}
},
dataType: 'JSONP' // <----
});
});
And json is like this:
{
"status": "ok", //<---- (chrome is saying problem is hare)
"count": 26,
"count_total": 26,
"pages": 1,
"products": [
{
"size": "16X18",
"id": 41,
"name": 86416,
"cost_price": 1200,
"sales_rate": 1300,
"description": "",
"remarks": "",
"batch_no": "NA"
}, {}...