代码之家  ›  专栏  ›  技术社区  ›  Ken Ingram

AJAX返回状态码0允许穿紧身衣这个AJAX实现不正确吗?

  •  1
  • Ken Ingram  · 技术社区  · 6 年前

    我有一个浏览器操作,我已经实现了它作为一个插件。 我正在测试我的域中的插件的构造 作为服务器和客户端的两台计算机之间。

    我陷入了AJAX失败的困境,状态代码为0。

    我设置了Apache以使CORS能够确保代码的行为符合预期。 我将这一行添加到apache配置并启用 headers :

    Header set Access-Control-Allow-Origin "*"

    javascript代码: 更新

    function sendData(data){
        var requestdata = 'data1=one&data2=two&data3=three';
        var xhr = new XMLHttpRequest();
    
        xhr.open("POST","http://server/test/test.php");
    
        xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
    
    xhr.onloadstart = function(){
        console.log("Onload status: " + xhr.status);
    }
    
    xhr.onerror = function(ev) {
        console.log("OnError Status: " + xhr.status + ", Loaded: " + ev.loaded + ", Total: " + ev.total);   
    }
    
    xhr.onreadystatechange = function() {
        console.log(xhr.status);
        if (xhr.status !== 200){
            console.log("Failure :(  + Status: " + xhr.status);
            //console.log(requestdata);
            console.log("Ready State: " + xhr.readyState + ", Status: " + xhr.status +", Response Text: " + xhr.responseText);
        } else {
            alert("Success!");
        }
    }
        xhr.send(requestdata);
    }
    

    测试.php

    <?php 
        require '../lib/Database.php';
        error_reporting(E_ALL);
        $db = new Database('test');
        var_dump($db);
    
        json_encode($_POST);
    ?>
    

    我是否正确地完成了这个AJAX实现? 如果我有,并且我已经对CORS进行了调整,为什么状态代码为0时失败?

    更新

    这是一个插件,我正在火狐测试。 从manifest.JS on中的“matches”参数中,导航到有问题的页面,使用DOM上的Vanilla JS从页面检索数据。

    收集的数据保存在一个对象中。

    数据对象已传递

    这是一张在浏览器中返回的图片: enter image description here

    更新2

    我用 http://httpbin.org/post 看看数据是否有任何进展不。

    0 回复  |  直到 6 年前
    推荐文章