这个ajax有一个action,可以用来运行函数并在回调函数中访问数据值作为$_POST['mydata']
$.ajax({
url: qrAjax.ajax_url,
type: 'POST',
context: this,
data: {
action: 'qrcode_post_create',
nonce: qrAjax.nonce,
mydata : datas,
},
success: function(response) {
alert("Success");
$(".qrresponse").append(response);
},
});
有没有办法用 fetch() 做类似的事情?
fetch('https://mywebsite.online/wp-json/wp/v2/qrcode', {
method: 'POST',
credentials: 'same-origin',
headers: new Headers({
'Content-Type': 'application/json;charset=UTF-8',
'X-WP-Nonce' : qrAjax.nonce
}),
body: JSON.stringify(OurPostData),
}).then(response => {
console.log(response);
return response.json();
}).then(data => console.log(data));