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