【发布时间】:2021-01-28 17:27:19
【问题描述】:
我有自己创建的JSON 文件,并想用这些数据填充select>option。但我收到Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at file:///C:/.../policy_app_policytype.json. (Reason: CORS request not http). 我该如何解决?这是我的代码:
index.html:
<div id="insurance-type">
<select name="insurance-type-select" id="insurance-type-select">
<option></option>
</select>
</div>
script.js:
$(document).ready(function () {
var url = "policy_app_policytype.json";
$.getJSON(url, function (data) {
$.each(data, function (index, value) {
$('#insurance-type-select').append('<option value="' + value.id + '">' + value.display_name + '</option>');
});
});
});
json:
[
{
"description": "Avtonəqliyyat vasitəsi sahiblərinin mülki məsuliyyətinin icbari sığortası şəhadətnaməsi",
"display_name": "AVSMMİS",
"id": 1,
"insurance_type": 1,
"is_other": 0,
"policy_type_id": 1,
"prefix": null,
"reasons_id": null,
"status": null,
"temp_prefix": null
},
{
"description": "Daşınmaz əmlakın icbari sığortası şəhadətnaməsi",
"display_name": "DƏİS",
"id": 2,
"insurance_type": 1,
"is_other": 0,
"policy_type_id": 2,
"prefix": null,
"reasons_id": null,
"status": null,
"temp_prefix": null
}
]
【问题讨论】:
标签: javascript html jquery json ajax