如何通过 $.post 方法发送查询参数?
How to send query parameters through $.post method?
我有一个问题如何将我的 JSON 字符串发送到 $.post 调用。由于 $.post 默认的 ContentType 是 application/x-www-form-urlencoded
但我的服务器只接受 application/json header
我在 $.post 电话中需要帮助的原因是我想要快速的结果
这里是我的示例代码供参考:
var jqxhr = $.post(url, sendData, function(data) {
console.log(data);
})
.done(function() {
})
.fail(function(data) {
console.log("Failed");
console.log(data);
})
.always(function() {
});
// Perform other work here ...
// Set another completion function for the request above
jqxhr.always(function() {
});
var obj = {
"timeout": "5s",
"_source": false,
"query": {
"nested": {
"path": "demo",
"query": {
"multi_match": {
"query": request,
"type": "phrase",
"operator": "and",
"fields": ["name"]
}
},
"inner_hits": {
"highlight": {
"fields": {
"name": {},
}
}
}
}
}
};
var sendData = JSON.stringify(obj);
$.post is a shorthand Ajax function
可以直接使用$.ajax()
$.ajax({
url: url,
type: 'post',
dataType: 'json',
contentType: 'application/json',
data: sendData // JSON string
});
或 使用$.ajaxSetup()
Sets default values for future Ajax requests. Its use is not
recommended.
$.ajaxSetup({
contentType: 'application/json',
});
$.post(url, sendData, function(data) {
});
注意: jqXHR.success()
、jqXHR.error()
和 jqXHR.complete()
回调在 jQuery 1.8
我有一个问题如何将我的 JSON 字符串发送到 $.post 调用。由于 $.post 默认的 ContentType 是 application/x-www-form-urlencoded
但我的服务器只接受 application/json header
我在 $.post 电话中需要帮助的原因是我想要快速的结果
这里是我的示例代码供参考:
var jqxhr = $.post(url, sendData, function(data) {
console.log(data);
})
.done(function() {
})
.fail(function(data) {
console.log("Failed");
console.log(data);
})
.always(function() {
});
// Perform other work here ...
// Set another completion function for the request above
jqxhr.always(function() {
});
var obj = {
"timeout": "5s",
"_source": false,
"query": {
"nested": {
"path": "demo",
"query": {
"multi_match": {
"query": request,
"type": "phrase",
"operator": "and",
"fields": ["name"]
}
},
"inner_hits": {
"highlight": {
"fields": {
"name": {},
}
}
}
}
}
};
var sendData = JSON.stringify(obj);
$.post is a shorthand Ajax function
可以直接使用$.ajax()
$.ajax({
url: url,
type: 'post',
dataType: 'json',
contentType: 'application/json',
data: sendData // JSON string
});
或 使用$.ajaxSetup()
Sets default values for future Ajax requests. Its use is not recommended.
$.ajaxSetup({
contentType: 'application/json',
});
$.post(url, sendData, function(data) {
});
注意: jqXHR.success()
、jqXHR.error()
和 jqXHR.complete()
回调在 jQuery 1.8