I'm having a lot of trouble debugging an ajax request. The same call works well on an older version of the site on the same server. It's a piece of code which sends data from Wordpress to Infusionsoft. I've checked and that's all connecting ok. I've also installed a CORS headers plugin thinking that might be the issue, but apparently not. With the following I get a parseerror:
var r=confirm("Are you sure you wish to create the '" + profile.title + "' email template?");
if (r==true) {
$('#mailout_buttons').hide();
$('#mailout_progress').show();
var request = $.ajax({
type : "post",
dataType : "jsonp",
url : ajaxurl,
data : $.extend(profile, {action: "create_weekly_email"}),
xhrFields: {
withCredentials: true
},
error: function(XHR, textStatus, errorThrown)
{
alert(textStatus);
},
success: function(response) {
if(response.type == "success") {
$('#mailout_buttons').show();
$('#mailout_progress').hide();
$('#mailout_success').show();
}
else {
alert("Error with template creation")
}
}
});
request.done(function(msg){ console.log(msg)});
request.fail(function(jqXHR, textStatus){ console.log("Request failed: " + textStatus)});
and if I change out the error to:
error: function(e)
{
alert(JSON.stringify(e), null, 4);
},
I get:
{"readyState":4,"responseText":"create{\"type\":\"success\"}","status":200,"statusText":"OK"}
I've also tried posting with text and html. Using html, it tells me the JSON is not well formed, although using the https://jsonformatter.curiousconcept.com/, it says it is. Using text, it falls through to the else in the success callback.
EDIT 1: I've posted the create_weekly_email function below. The create_weekly_email calls get_weekly_email which has an sql query in it. I've hard coded a couple of vars into it, which return results in both phpmyadmin and in the responseText var. I can see that there is some malformed html returned ( which creates an office document ), and the length of the html varies. I don't want to post returned data here, but in this last call it's missing all the closing tags. Sometimes the returned string is much shorter, which makes me think there might be some kind of timeout issue?
function create_weekly_email() {
if(empty($_SERVER['HTTP_X_REQUESTED_WITH']) || strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) != 'xmlhttprequest') {
header("Location: " . $_SERVER["HTTP_REFERER"]);
exit;
}
$result = ["type" => "error"];
if($profile = filter_input_array(INPUT_POST, massemail_fields())) {
if($re = create_weekly_mail_template($profile)) {
$result = ["type" => "success"];
}
}
wp_send_json($result);
exit;
}