错误:键是无效的 JQuery 语法,因为它缺少右括号



我在 chrome 控制台中收到此错误,同时尝试请求我正在使用的政府私有 API 测试服务器的响应。它说语法是错误的,但我根本没有看到缺少的括号。 Edge 控制台给出额外的错误文本

BAD REQUEST - The request could not be processed by the server due to invalid syntax.

否则,Edge 与 chrome 中的错误相同:

{"Message":"The request is invalid.",
"ModelState":{"message":[
"The key is invalid JQuery syntax because it is missing a closing bracketrnParameter name: key"]}},
"status":400,
"statusText":"Bad Request"}

我只是在编写一个测试请求,尝试将 xml 表单发布到测试服务器上的预期消息收件箱。(政府测试服务器不是我的)

我已经尝试了很多变体,并查看了 api 文档以获取可能的解释。 这是代码:

<script>
$(document).ready(function() {
var postData = "";
var xmltext = "";
$.get('TiltakUtenAnsvarsrett.xml', function(tiltakutenansvarsrettXML) {
xmltext = tiltakutenansvarsrettXML;
//xmltest = xmltext.replace(/"/g, '\"');
console.log("Dette er xmltext variablen" + xmltext);
//console.log("Dette er xmltext variablen stringified" + JSON.stringify(xmltext));
}, 'text');
$("#formtext").click(function() {
this.postData = {
"Type": "FormTask",
"ServiceCode": "4373",
"ServiceEdition": "1",
"_embedded": {
"forms": [{
"Type": "MainForm",
"DataFormatId": "5508",
"DataFormatVersion": "41083",
"FormData": xmltext
}
],
"attachments": [{
"Filename": "String example",
"Attachmenttype": "String example 2",
"Data": "Base64 encoded"
}]
}
};
$.ajax({
url: "https://xxx.xxxx.xx/api/my/messages",
headers: {
"Accept": "application/hal+json",
"ApiKey": "xxxxxxx",
},
xhrFields: {
withCredentials: true
},
type: "POST",
data: JSON.stringify(this.postData),
success: function(data, status, jqHxr) {
console.log("====== Returned messages ======");
console.log(JSON.stringify(data));
}.bind(this),
error: function(jqHxr, textStatus, errorThrown) {
console.log("Kaster feil status");
console.log("jqHxr er: " + JSON.stringify(jqHxr) + "n");
console.log("Status er: " + textStatus + "n");
console.log("Erroren er: " + errorThrown + "n");
console.log("postData er: " + JSON.stringify(this.postData) + "n");
console.log("Responsetext er: " + jqHxr.responseText);
console.log();
}.bind(this)
});
});
});
</script>

谷歌搜索只给出了这种错误消息的少数情况,不足以继续下去。

如果服务需要 JSON,请添加 contentType json

type: "POST",
contentType:"application/json",
data: JSON.stringify(this.postData),
success: function(data, status, jqHxr) {

你关闭括号"}"以快速。 这是固定代码:

{"Message":"The request is invalid.",
"ModelState":{"message":[
"The key is invalid JQuery syntax because it is missing a closing bracketrnParameter name: key"]},
"status":400,
"statusText":"Bad Request"}

您可以在此页面上测试 json: http://www.jsoneditoronline.org/

相关内容

最新更新