javascript validation運行錯誤,ajaxsetup無效_javascript – $.ajaxSetup無效

 2023-11-19 阅读 27 评论 0

摘要:$.ajaxSetup為將來的Ajax請求設置默認值.Its use is not recommended as suggested in the JQuery documentation.無論如何,因為它設置了將來調用的默認值,它必須在所有依賴于這些默認值的ajax調用之前執行.例如,如果您沒有提到調用的url,則$ajaxSetup中配置的默認URL將是調用

$.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()調用的圖片

RTcJd.jpg

$.get()調用的圖片

SWTq2.jpg

因此,如果要設置標題,只需使用$.ajax()而不是$.get()

希望這可以幫助 :)

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://808629.com/184073.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 86后生记录生活 Inc. 保留所有权利。

底部版权信息