![](/img/trans.png)
[英]400 Bad Request error when sending POST request to WCF service using JavaScript
[英]Bad Request Error on POST request in Javascript
我正在嘗試執行POST請求,但收到“錯誤請求”錯誤。 如果有人能告訴我我在這里做錯了,我將不勝感激。
function mkSentiTopics() {
dta['n_clusters'] = 3;
$.ajax({
type: "POST",
url: "http://saxonydemoubuntu.southcentralus.cloudapp.azure.com/sentitopic",
data: JSON.stringify(dta),
dataType: "json",
contentType: "application/json; charset=utf-8",
success: function(response) {
alert("success");
},
beforeSend: function() {
},
error: function(xhr, stats) {
alert("error");
}
});
}
“ dta”是一本字典
// dta["sentis"] is a list of numbers
// dta["texts"] is a list of strings
// dta["n_clusters"] is an int.
該錯誤表明您要發送到服務器的有效負載的結構與預期的不匹配,因此將其拒絕。 不知道API簽名,我們對此無能為力。
但是,我將在黑暗中拍照,並根據您發帖的這一部分提出以下建議:
// dta["sentis"] is a list of numbers
// dta["texts"] is a list of strings
// dta["n_clusters"] is an int.
當您使用JSON.stringify(dta)
,請確保輸出與 API期望的輸出完全匹配 ,並注意JSON僅知道布爾值,數組,對象,字符串和數字,並進一步注意true !== "true"
, 1 !== "1"
,依此類推。
例:
let dta = { // Note object wrapper sentis: [1, 2, 3, 4], // Note lack of "" texts: ["a", "b", "c"], // Note presence of "" n_clusters: 5 // 'Naked' number } console.log(JSON.stringify(dta, 0, 1)); // Note that "dta" is not a root key
如果您的API要求存在根密鑰“ dta”或結構不同,請相應地修改dta
對象。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.