Ajax 在IE上查詢失敗

Ajax 在Chrome, Edge都可以正常執行,但在IE上卻失敗。

原因是ajax在Chrome, Edge對於中文字會自動轉換編碼,但是在IE並不會自動轉換而形成亂碼。

解決方式:

方法一 : 在js中使用encodeURIComponent先將中文做編碼後,再執行ajax

1
2
3
4
5
6
7
8
9
10
11
12
13
function setupPayItemList(classValue, itemValue) {
var url = "{0}/BPM/Forms/Fetcform1003/getPayItemList?formkind={1}&formNo={2}&classType={3}".format(window.baseUrl, _opts.model.formKind, _opts.model.formNo, encodeURIComponent(classValue));

$.ajax({
url: url,
contentType: "application/json;",
type: 'post',
dataType: 'json',
async: false,
success: function (response) {
}
});
}

方法二 : 將參數值,使用json格式傳送

1
2
3
4
5
6
7
8
9
10
11
12
13
14
function updateMaterialName(materialNum) {
$.ajax({
url: "{0}/BPM/Forms/Fetcform1004/getMaterialInfo?formkind={1}&formNo={2}".format(window.baseUrl, _opts.model.formKind, _opts.model.formNo),
contentType: 'application/json',
type: 'post',
dataType: 'json',
data: JSON.stringify({
'materialNum': materialNum
}),
success: function (response) {

}
});
}
作者

Nick Lin

發表於

2021-12-01

更新於

2023-01-18

許可協議


評論