试图访问json数据但得到一个crossorigin请求错误

问题描述 投票:0回答:1

我试图使用以下jquery ajax函数访问以下数据。

$.ajax({
type: 'GET',
url: '/data/hipstercoffee',
success: function(data) {
  console.log('success', data);
}

});

当我运行该功能时,它无法加载该文件。我得到的错误是“无法加载文件,只支持交叉原始请求.....”json数据的格式如下。

[
{
"shopName": "The Coffee Connection",
"address": "123 Lakeside Way",
"phone": "16503600708",
"prices": [
  {
    "Cafe Latte": 4.75,
    "Flat White": 4.75,
    "Cappucino": 3.85,
    "Single Espresso": 2.05,
    "Double Espresso": 3.75,
    "Americano": 3.75,
    "Cortado": 4.55,
    "Tea": 3.65,
    "Choc Mudcake": 6.40,
    "Choc Mousse": 8.20,
    "Affogato": 14.80,
    "Tiramisu": 11.40,
    "Blueberry Muffin": 4.05,
    "Chocolate Chip Muffin": 4.05,
    "Muffin Of The Day": 4.55
  }
]
}
]

这个函数是否适用于这种格式化的json文件?

javascript jquery json ajax
1个回答
0
投票

我设法搞清楚了。我应该一直在运行MAMP服务器。另外,网址不正确。它应该是

url: '/data/hipstercoffee.json',
© www.soinside.com 2019 - 2024. All rights reserved.