1. 程式人生 > >解決“No 'Access-Control-Allow-Origin' header is present on the requested resource”

解決“No 'Access-Control-Allow-Origin' header is present on the requested resource”

1. 問題描述

在js中使用ajax請求在網頁控制檯下列印以下錯誤資訊:

XMLHttpRequest cannot load http://192.168.2.46:8000/account/getjson/. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:63342' is therefore not allowed access.

本人的ajax程式碼如下

var jsontree = [];
$.ajax({
    url: "http://192.168.2.46:8000/account/getjson/"
, type: "GET", dataType: 'JSON', success: function(result){ jsontree = result; } });

2. 解決方法

將上面的dataType: 'JSON'替換為dataType: 'JSONP'即可。

OK, Enjoy it!!!