I have this bit of jQuery doing an ajax call. The script was having issues in IE and Chrome on Win 8.1 only, however, i changed a few things (including ommitting a closing php tag that might have been causing issues) and now IE works and on my Win 8.1 on Chrome it works, but the client is still reporting issues.
Can anyone see a problem with my script that would cause this to fail on Win 8.1 on Chrome ONLY? (BTW, Safari and Opera were probably not tested by client so may also be failing on those).
I have literally read for hours and hours on SO and other question/answer sites, as well as many different related forums etc. I have tried many of the different suggestions with no luck.
Obviously, the first thing people think is Cross Domain issue, which the site is hosted on an "add-on" domain, but I'm calling the script at the same URL. Should I be calling it using the main domain URL instead? (I can't figure out what that main domain is at the moment, see this question: Find original main domain name based on add-on domain name)
Client is getting alerts from the ajax error block:
readystate = 0
status = 0
responseText = ''
statusText = 'error'
Any help is much appreciated, I've been stuck on this for weeks now.
Here is my jQuery:
$('#bkdl-submit').on('click', function(e) {
e.preventDefault();
var tempvar = $('#bkdl-email').val();
if (IsEmail(tempvar)) {
$('#loading-image').show();
$('#bkdl-submit').hide();
$.ajax({
url: "http://addondomain.com/wp-content/themes/html/bkdl-ajax.php?nocache="+Date.now(),
method: "POST",
data: {
email: tempvar
},
success: function (data) {
$("#bkdl-email").css('background-color', '#f00');
$("#bkdl-email").val('');
$("#hidlink").attr('href', data);
$("#hidlink").text('Click to Download');
$("#hidlink").show();
$("#bkdl-submit").hide();
},
complete: function () {
$('#loading-image').hide();
},
error: function (xhr, strError, strHttpStatus) {
alert(JSON.stringify(xhr, null, 2));
alert(strHttpStatus);
}
});
} else {
alert('Invalid Email');
}
return false;
});
The AJAX PHP script in case that helps...
<?php
define('IS_AJAX', isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest');
if(!IS_AJAX) {die('Restricted access1');}
$pos = strpos($_SERVER['HTTP_REFERER'],getenv('HTTP_HOST'));
if($pos===false) { die('Restricted access'); }
require_once 'Ctct/autoload.php';
use Ctct\ConstantContact;
use Ctct\Components\Contacts\Contact;
use Ctct\Components\Contacts\ContactList;
use Ctct\Components\Contacts\EmailAddress;
use Ctct\Exceptions\CtctException;
define("APIKEY", "xxxxxxxxxxxxxxxxxxxxxxxxxx");
define("ACCESS_TOKEN", "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
$cc = new ConstantContact(APIKEY);
// check if the form was submitted
if (isset($_POST['email']) && strlen($_POST['email']) > 1) {
$email = strip_tags(htmlentities(trim($_POST['email'])));
$action = "Getting Contact By Email Address";
try {
// check to see if a contact with the email addess already exists in the account
$response = $cc->getContactByEmail(ACCESS_TOKEN, $email);
// create a new contact if one does not exist
if (empty($response->results)) {
$action = "Creating Contact";
$contact = new Contact();
$contact->addEmail($_POST['email']);
$contact->addList('xxxxxxxxxxxxx');
$contact->first_name = '';
$contact->last_name = '';
$returnContact = $cc->addContact(ACCESS_TOKEN, $contact);
// update the existing contact if address already existed
} else {
$action = "Updating Contact";
$contact = $response->results[0];
$contact->addList('xxxxxxxxxxxx');
$contact->first_name = '';
$contact->last_name = '';
$returnContact = $cc->updateContact(ACCESS_TOKEN, $contact);
}
echo 'http://addondomain.com/wp-content/themes/html/giveaway.pdf';
// catch any exceptions thrown and email to dev
} catch (CtctException $ex) {
$errorvar = print_r($ex->getErrors(), true);
mail('deverrors@gmail.com', 'test', $errorvar);
die();
}
} else {
echo 'Invalid Email'; exit;
}
exit;