$.ajaxSetup為將來的Ajax請求設置默認值.
Its use is not recommended as suggested in the JQuery documentation.
無論如何,因為它設置了將來調用的默認值,它必須在所有依賴于這些默認值的ajax調用之前執行.例如,如果您沒有提到調用的url,則$ajaxSetup中配置的默認URL將是調用的url.如果您所做的調用取決于那些默認值,那么此代碼
javascript validation運行錯誤,self.authenticate = function () {
self.token = sessionStorage.getItem(tokenKey);
var headers = {};
if (self.token) {
you need to enable javascript、headers.Authorization = 'Bearer ' + self.token;
$.ajaxSetup({
headers: headers
});
javascript錯誤解決方案。}
}
必須在進行以下調用之前執行.
self.getUsers = function () {
請啟用 JavaScript。$.get("../API/Users/GetUsers/");
}
現在檢查一下
*************** Plunker for answer ****************
在那個plunker中,按F12鍵進入開發者控制臺中的網絡選項卡,然后檢查$.ajax()和$.get()進行的調用中的標題.
我在觀察到的那個(要點讀)點,
>如果調用是$.ajax(),那么標題顯示,并且調用的url是$.ajaxSetup中提到的url
>如果調用是$.get(),那么標題不顯示,調用的url是plunker url,意味著它將是http:// MySite / etc.
$.ajax() is the most configurable one, where you get fine grained
control over HTTP headers and such. You’re also able to get direct
access to the XHR-object using this method. Slightly more fine-grained
error-handling is also provided. Can therefore be more complicated and
often unecessary, but sometimes very useful. You have to deal with the
returned data yourself with a callback.
$.get() is just a shorthand for $.ajax() but abstracts some of the
configurations away, setting reasonable default values for what it
hides from you. Returns the data to a callback. It only allows
GET-requests so is accompanied by the $.post() function for similar
abstraction, only for POST
有關更多信息
如果你愿意,可以測試一下.
$.ajax()調用的圖片
$.get()調用的圖片
因此,如果要設置標題,只需使用$.ajax()而不是$.get()
希望這可以幫助 :)
版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。
工作时间:8:00-18:00
客服电话
电子邮件
admin@qq.com
扫码二维码
获取最新动态