ajax在jsonp中無法使用async:false

因為瀏覽器的安全性限制,已無法跨網域取存,所以出現了jsonp,利用Callback機制逃過限制。
但是因為jsonp使用的機制與json不同,所以在使用.ajax時就出現了問題。

為了要在自訂的function內傳回.ajax的資料,會設定async:false來解決同步的問題。
但是.ajax無法在jsonp設定async:false,所以傳回的資料會是空的。
例如:
<pre class="lang:default decode:true ">var r;
$.ajax({
url: OpenData.url,
data: OpenData.data(),
dataType: 'jsonp',
async:false
success: function(data) {
r = data ;
}
});
return r;</pre>
&nbsp;

雖然已經設定async:false,r還是空值。

jQuery.ajax() Document:
(<a href="http://api.jquery.com/jquery.ajax/">http://api.jquery.com/jquery.ajax/</a>)
async (default: true)
Type: Boolean
By default, all requests are sent asynchronously (i.e. this is set to true by default).
If you need synchronous requests, set this option to false.
Cross-domain requests and dataType: "jsonp" requests do not support synchronous operation.
Note that synchronous requests may temporarily lock the browser,
disabling any actions while the request is active.

Windows 11安裝時跳過網路連線