SAPUI5內建的AJAX類庫,在提交POST請求的時候,忽略了http headers。GET請求沒有這個問題。
下面的代碼不能提交HTTP Headers
sUrl = getSMPURL() + "/odata/applications/latest/com.sap.flight.kapsel/Connections";$.ajax({ type : "POST", cache : false, url: sUrl, data: payload, beforeSend : function(request) { request.setRequestHeader('Content-Type', "application/xml"); }, success : function(data, ajaxOptions, xhr) { alert("passed register"); }, error : function(xhr, ajaxOptions, thrownError) { alert("shit"); }, xhrFields : { withCredentials : true, }});
下面的代碼可以提交HTTP Headers
這裡寫代碼片
sUrl = getSMPURL() + “/odata/applications/latest/com.sap.flight.kapsel”;
$.ajax({
headers: {
‘Authorization’: “Basic ” + btoa(getUserName() + “:” + getPassword())
},
type : “GET”,
cache : false,
url: sUrl,
dataType:’xml’,
beforeSend : function(request) {
}, success : function(data, ajaxOptions, xhr) { alert("passed authentication"); }, error : function(xhr, ajaxOptions, thrownError) { alert("shit"); }, xhrFields : { withCredentials : true, }});
“`