我正在使用访存API语法对admin-ajax.php进行Ajax调用。这是调用后端脚本的代码:
fetch(ajax_obj.ajaxurl, {
method: "POST",
mode: "cors",
cache: "no-cache",
credentials: "same-origin",
body: JSON.stringify(this.data),
headers: {
"Content-Type": "application/json"
}
})
,并收到400错误请求响应。
有人可以告诉我这个请求在哪里不合适吗?
检查Chrome中的网络开发工具时,我可以看到发送的正文正常,URL也正常... ...据我所知,4xx状态代码是针对客户端错误的,所以我什至不看服务器端代码...如果我错了,请对此提供反馈...
实际上,我有这样的jQuery ajax调用:
this.data = {
'action': 'ajax_product_query',
'locations': this.locations,
'type': this.category != '' ? this.category : [],
'all-locations': this.filters['locationFilter'].all.checked,
'page': ajax_obj.current_page
};
$.ajax({
url: ajax_obj.ajaxurl,
method: 'post',
dataType: 'json',
data: this.data,
beforeSend: function(xhr) {
button.innerHTML = 'Loading...';
},
success: (data) => { ...
...它就像一个魅力...
然后,愿意删除jQuery依赖关系,想要将jQuery ajax调用转换为Fetch API语法,如下所示:
fetch(ajax_obj.ajaxurl, {
method: "POST",
mode: "cors",
cache: "no-cache",
credentials: "same-origin",
body: JSON.stringify(this.data),
headers: {
"Content-Type": "application/json"
}
})
.then(response => response.json()) ...
然后请求变成了400个错误请求...
谢谢!
答案 0 :(得分:0)
因为在JS提取“ POST”请求后,您无法在Php中获得$ _POST数据。脏溶液。您可以在u“ /wp-admin/admin-ajax.php”中添加此代码来解决此问题:
Sub StartWriting()
Dim fsT As Object
Set fsT = CreateObject("ADODB.Stream")
fsT.Type = 2
fsT.Charset = "utf-8"
fsT.Open
fsT.WriteText "special characters: äöüß"
ContinueWriting fsT, "SomeId"
fsT.SaveToFile sFileName, 2
End Sub
Sub ContinueWriting(fs as Object, id as Variant)
'do something with id
fs.WriteText "In ContinueWriting"
End Sub
或者尝试找到更好的解决方案。
附言对不起,我的英语