我有一个简单的ajax post方法将数据发送到Web api,但即使控制台中没有错误,此代码也不会发布数据。 请帮我解决这个问题我已经用 jQuery 更改 $ ,如描述Here。我正在使用 Wordpress 4.9 。 这是用wordpress编写的源代码。
代码
var ServiceAndVisitroData = {
//orderid
selectedService: selecteddata,
userId: $('#userid').val().trim(),
OrderId: $('#orderid').val().trim(),
DealInfo: $('#deals').val().trim(),
IPAddress: userip,
deviceName: userDevicedata[0],
OSName: jscd.os + "," + jscd.osVersion,
browserName: jscd.browser + "," + jscd.browserMajorVersion + "," + jscd.browserVersion,
IsCustomllogo: false,
userToken: cookiesplit[1],
firstname: cookiesplit[2],
lastname: last[0]
};
$.ajax({
type: "POST",
url: 'http://test.com/api/agency/postcheckout',
// contentType: "application/json; charset=utf-8",
//data: JSON.stringify(ServiceAndVisitroData),
data: ServiceAndVisitroData,
dataType: "json",
success: function (result) {
// alert(result.firstname);
// alert(result.lastname);
// alert(result.tokenn);
// alert(result.useridd);
if (result.success == "pymt") {
window.location.href = 'http://test.com/api/agency/payment/' + result.Id + '/' + result.Showbutton + '/' + result.firstname + '/' + result.firstname + '/' + result.lastname
return false;
}
else if (result.success == "ord") {
window.location.href = 'http://test.com/api/agency/order/' + result.Id + '/' + result.Showbutton + '/' + result.firstname + '/' + result.lastname;
return false;
}
else {
// alert('Your form has been submitted.');
//alert(result.id);
window.location.href = '/';
}
},
error: function (result) {
alert('Oh no ');
window.location.href = result.Url;
}
});
答案 0 :(得分:1)
你必须像这样使用
var ServiceAndVisitroData = {
//orderid
selectedService: selecteddata,
userId: $('#userid').val().trim(),
OrderId: $('#orderid').val().trim(),
DealInfo: $('#deals').val().trim(),
IPAddress: userip,
deviceName: userDevicedata[0],
OSName: jscd.os + "," + jscd.osVersion,
browserName: jscd.browser + "," + jscd.browserMajorVersion + "," + jscd.browserVersion,
IsCustomllogo: false,
userToken: cookiesplit[1],
firstname: cookiesplit[2],
lastname: last[0]
};
jQuery.ajax({
url: "<?php echo admin_url( 'admin-ajax.php' );?>",
data: {action: 'ajax_call_back_func', data:ServiceAndVisitroData},
type: "POST",
success: function(result, textStatus, XMLHttpRequest){
if (result.success == "pymt") {
window.location.href = 'http://test.com/api/agency/payment/' + result.Id + '/' + result.Showbutton + '/' + result.firstname + '/' + result.firstname + '/' + result.lastname
return false;
}
else if (result.success == "ord") {
window.location.href = 'http://test.com/api/agency/order/' + result.Id + '/' + result.Showbutton + '/' + result.firstname + '/' + result.lastname;
return false;
}
else {
// alert('Your form has been submitted.');
//alert(result.id);
window.location.href = '/';
}
},
error: function (result) {
alert('Oh no ');
window.location.href = result.Url;
}
});
在functions.php中
function ajax_call_back_func(){
print_r($_POST);exit;
}
add_action('wp_ajax_ajax_call_back_func', 'ajax_call_back_func');
add_action('wp_ajax_nopriv_ajax_call_back_func', 'ajax_call_back_func');