【问题标题】:jQuery Ajax readystate 0 status 0 in Windows 8 (8.1) on Chrome ONLYjQuery Ajax readystate 0 status 0 in Windows 8 (8.1) on Chrome ONLY
【发布时间】:2015-03-22 05:48:25
【问题描述】:

我有一点 jQuery 做一个 ajax 调用。该脚本仅在 Win 8.1 上的 IE 和 Chrome 中出现问题,但是,我更改了一些内容(包括省略可能导致问题的关闭 php 标签),现在 IE 可以正常工作,并且在 Chrome 上的 Win 8.1 上可以正常工作,但是客户仍在报告问题。

任何人都可以看到我的脚本存在的问题,该问题会导致仅在 Chrome 上的 Win 8.1 上失败吗? (顺便说一句,Safari 和 Opera 可能没有经过客户端测试,因此也可能在这些方面失败)。

我已经在 SO 和其他问答网站以及许多不同的相关论坛等上阅读了数小时。我尝试了许多不同的建议,但都没有运气。

显然,人们首先想到的是跨域问题,该站点托管在“附加”域上,但我在同一个 URL 调用脚本。我应该使用主域 URL 来调用它吗? (我现在不知道那个主域是什么,看这个问题:Find original main domain name based on add-on domain name

客户端收到来自 ajax 错误块的警报:

readystate = 0
status = 0
responseText = ''
statusText = 'error'

非常感谢任何帮助,我已经坚持了好几个星期了。

这是我的 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;
            });

AJAX PHP 脚本以防万一……

<?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;

【问题讨论】:

  • method: "POST", 不应该是type: "POST",
  • type (default: 'GET') Type: String 方法的别名。如果您使用的是 1.9.0 之前的 jQuery 版本,则应该使用 type。 (我使用的是 1.11.1 但我还是会尝试)
  • document.domain 将引用正确的。
  • 感谢投反对票的人。真正站起来的人,会毫无价值地四处对人们的问题投反对票。获得生命

标签: jquery ajax google-chrome windows-8.1 readystate


【解决方案1】:

这实际上是 ajax 文件末尾的小 ?&gt;

我知道您应该忽略它们,并且每当我在文件底部看到它们时,我都会尝试删除它们,但我从来不知道它会导致像这样的问题,与 ajax 调用有关。

显然,微软在 win8 中的新安全策略与此有关。我仍然不太清楚它是如何发挥作用的,但这里有人告诉我。

所以是的...今天的教训...

不要用 PHP 结束标签关闭你的 PHP 文件?>否则你可能会在 win8 中遇到问题

【讨论】:

    猜你喜欢
    • 2013-10-24
    • 2018-12-24
    • 2011-10-24
    • 2014-12-17
    • 1970-01-01
    • 2016-05-20
    • 2012-12-30
    • 2014-09-21
    • 2014-09-10
    相关资源
    最近更新 更多