Php 为什么Jquery GET请求不返回数据?

Php 为什么Jquery GET请求不返回数据?,php,jquery,greasemonkey,Php,Jquery,Greasemonkey,code.php: $.get("http://localhost/code.php", function(data){ alert(data); }); 我还尝试了post和ajax的其他变体,但没有任何效果。我可以很好地运行php脚本,例如,我可以写入文件,但是它们不会返回任何数据 我正在IIS服务器上运行脚本 [编辑] 我忘了添加一个重要的细节,我正在从greasemonkey脚本调用php脚本。我在服务器上试过,效果很好。但我需要这个给greasemonkey 如果通过Grease

code.php:

$.get("http://localhost/code.php", function(data){
alert(data);
});

我还尝试了post和ajax的其他变体,但没有任何效果。我可以很好地运行php脚本,例如,我可以写入文件,但是它们不会返回任何数据

我正在IIS服务器上运行脚本

[编辑]


我忘了添加一个重要的细节,我正在从greasemonkey脚本调用php脚本。我在服务器上试过,效果很好。但我需要这个给greasemonkey

如果通过GreaseMonkey
从远程页面调用方法,则必须添加前缀unsafeWindow.`

<?php
echo "hello!";
?>
如果您不需要特定于JQuery的方法,我建议您使用:

从中可以看出,Greasemonkey不提供jQuery使用的XHR对象,因此您必须包装它提供的内容,并使用自定义实现设置jQuery AJAX

我在此引述有关守则:

GM_xmlhttpRequest({
    "method": "get",
    "url": "http://localhost/code.php",
    "onload": function(data){
        alert(data);
    }
})
要设置jQuery的AJAX,请执行以下操作:

// Wrapper for GM_xmlhttpRequest
function GM_XHR() {
    this.type = null;
    this.url = null;
    this.async = null;
    this.username = null;
    this.password = null;
    this.status = null;
    this.headers = {};
    this.readyState = null;

    this.open = function(type, url, async, username, password) {
        this.type = type ? type : null;
        this.url = url ? url : null;
        this.async = async ? async : null;
        this.username = username ? username : null;
        this.password = password ? password : null;
        this.readyState = 1;
    };

    this.setRequestHeader = function(name, value) {
        this.headers[name] = value;
    };

    this.abort = function() {
        this.readyState = 0;
    };

    this.getResponseHeader = function(name) {
        return this.headers[name];
    };

    this.send = function(data) {
        this.data = data;
        var that = this;
        GM_xmlhttpRequest({
            method: this.type,
            url: this.url,
            headers: this.headers,
            data: this.data,
            onload: function(rsp) {
                // Populate wrapper object with returned data
                for (k in rsp) {
                    that[k] = rsp[k];
                }
            },
            onerror: function(rsp) {
                for (k in rsp) {
                    that[k] = rsp[k];
                }
            },
            onreadystatechange: function(rsp) {
                for (k in rsp) {
                    that[k] = rsp[k];
                }
            }
        });
    };
};

在这一步之后,
.get()
和其他AJAX方法应该可以很好地工作。

好吧,最后我放弃了greasemonkey,因为无论我尝试什么,它都无法工作。所以我有了脚本,幸运的是,我所需要的代码只需点击一下插件页面。如果其他人需要它,这里就是:

$.ajaxSetup({
    xhr: function(){return new GM_XHR;}
});

它在脚本语言中可以立即工作,不需要包含任何其他内容。

因此,如果您转到,您会看到什么?另外,我假设您也在从提供服务的页面调用它,对吗?您的JS控制台说什么?有错误吗?你的服务器日志呢?您能确认它正在接收请求吗?浏览器开发工具有一个“网络”选项卡,您可以在其中查看发出了哪些请求以及响应中包含了什么内容……我为简短的回复表示歉意。我删除了它并将其放在评论中。非常感谢。这里是:我把你的代码放在正确的上下文中,我的工作很好<代码>$。获取(“http://localhost/code.php“,函数(数据){警报(数据);}”
$.ajaxSetup({
    xhr: function(){return new GM_XHR;}
});
var ret = GM_xmlhttpRequest({
  method: "GET",
  url: "http://localhost/code.php",
  onload: function(res) {
    alert(res.responseText);
  }
});