简体   繁体   English

对PHP的Javascript发布方法XHR请求失败

[英]Javascript post method XHR request to PHP fails

I am trying to do a simple post to a PHP page using JavaScript's XHR in Chrome (latest). 我正在尝试在Chrome(最新)中使用JavaScript的XHR对PHP页面进行简单的发布。 I am getting errors - specifically, the test.php page I am calling (in the same folder as my JS code, locally), is showing as a failed http response. 我遇到了错误-特别是,我正在调用的test.php页面(与本地JS代码位于同一文件夹中)显示为失败的http响应。 Chrome Dev Tools shows as 'Failed to load response data'. Chrome开发工具显示为“无法加载响应数据”。

test.php test.php

<?php
echo 'this string should return';
?>

ajax.js ajax.js

createXHR: function () {
        if (typeof XMLHttpRequest !== 'undefined') {
            return new XMLHttpRequest();
        } else if (typeof ActiveXObject !== 'undefined') {
            if (typeof arguments.callee.activeXString !== 'string') {
                var versions = ['MSXML2.XMLHttp.6.0', 'MSXML2.XMLHttp.3.0',
                    'MSXML2.XMLHttp'],
                        i,
                        len;
                for (i = 0, len = versions.length; i < len; i++) {
                    try {
                        new ActiveXObject(versions[i]);
                        arguments.callee.activeXString = versions[i];
                        break;
                    } catch (ex) {
                        //skip
                    }
                }
            }
            return new ActiveXObject(arguments.callee.activeXString);
        } else {
            throw new Error('No XHR object available.');
        }
    },

var xhr = AJAX.createXHR();

        /**
         *  @event onreadystatechange
         *  @description DOM Level 0 event fired when readyState property is changed
         */
        xhr.onreadystatechange = function () {
            /** @param {Number} readyState 0 - Uninitialized, 1- Open, 2- Sent, 3- Receiving, 4- Complete */
            if (xhr.readyState === 4) {

                console.log(xhr.status); //prints 0

                if ((xhr.status >= 200 && xhr.status < 300) || xhr.status === 304) {
                    AJAX.success(xhr.responseText);
                } else {
                    //fail
                    AJAX.fail();
                }
            }
        };

        xhr.open('post', 'test.php', true);

        xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');

        xhr.send('name=myname&email=myemail');

I can GET this page, so I can definitely see the page on the same origin, but the POST just doesn't seem to work. 我可以获取此页面,因此我绝对可以看到该页面具有相同的来源,但是POST似乎不起作用。 I have set the Content-Type, set async as true, set the method as post, and the file name is correct. 我已经设置了Content-Type,将async设置为true,将方法设置为post,并且文件名正确。 I tried using return in my php file, and print_r and echo - no dice. 我尝试在我的php文件中使用return,并使用print_r和echo-没有骰子。

Why is the response not coming through at all? 为什么反应根本没有通过? It just gets through to readyState = 4 = Complete, but the xhr.status is 0 (zero). 它只是进入readyState = 4 = Complete,但是xhr.status为0(零)。 Thoughts? 有什么想法吗?

EDIT: 编辑:

I am using localhost, run using netbeans. 我正在使用localhost,使用netbeans运行。

http://localhost:8383/Coding%20Practice/public_html/index.html http:// localhost:8383 / Coding%20Practice / public_html / index.html

test.php is at http://localhost:8383/Coding%20Practice/public_html/test.php test.php位于http:// localhost:8383 / Coding%20Practice / public_html / test.php

Running a local file from netbeans is NOT a server. 从netbeans运行本地文件不是服务器。 I thought it acted like a server, but it does not. 我认为它的行为就像一台服务器,但事实并非如此。 Running this code through wamp works fine. 通过wamp运行此代码可以正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM