Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript jquery承诺的问题_Javascript_Jquery - Fatal编程技术网

Javascript jquery承诺的问题

Javascript jquery承诺的问题,javascript,jquery,Javascript,Jquery,我正在实践jquery承诺,在我的演示代码中发生了一些奇怪的事情。我的代码所做的是当我点击按钮时,它从服务器接收一个简单的json数据。我使用了两个jquery承诺,一个是done(),第二个是fail()。当我点击按钮时,它从服务器接收数据,但done()没有执行。数据通过fail()显示在控制台中。为什么以及如何解决这个问题?下面是我的代码 jquery var Obj = function () { return {

我正在实践jquery承诺,在我的演示代码中发生了一些奇怪的事情。我的代码所做的是当我点击按钮时,它从服务器接收一个简单的json数据。我使用了两个jquery承诺,一个是
done()
,第二个是
fail()
。当我点击按钮时,它从服务器接收数据,但
done()
没有执行。数据通过
fail()
显示在控制台中。为什么以及如何解决这个问题?下面是我的代码

jquery

        var Obj = function () {
            return {
                gets: function (successHandler, errorHandler) {
                    console.log('hello');
                    return $.ajax({
                        url: '/server.php',
                        dataType: 'JSON',
                        type: 'GET'
                    });
                }
            }
        };

        $('.button').on('click', function () {
            var obj = new Obj();
            var promise = obj.gets();

            promise.done(function (data) {
                console.log(data);
            });

            promise.fail(function (e) {
                console.log(JSON.stringify(e)); //this logs below
            });
输出

{"readyState":4,"responseText":"<?php\n$response = array('oranges', 'apples', 'berries');\nexit(json_decode($response));","status":200,"statusText":"OK"} 
但是


“responseText”:“您的服务器似乎没有将
server.php
解析为php文件。您在
responseText
中获得原始php代码。请配置服务器,使其运行
php
代码。如果无法运行,则这将是另一个带有
php
标记的问题;)
$response = array('oranges', 'apples', 'berries');
exit(json_decode($response));
return $.ajax({
   url: '/server.php'
   dataType: 'JSON',
   ...
"responseText":"<?php\n$response = array ...