[英]jQuery ajax call to php process form page not returning success or error
[英]PHP function returning 'null' for success method in jQuery.ajax call
我有一個與此非常類似的問題: jquery json函數返回null 。
但是,我遵循了上面提到的建議,結果仍然看到null
。
這是我的代碼:
JS:
Gallery.prototype.getImages = function(opt){
var self = this;
var baseurl = 'http://local.gallery.dev'
$.ajax({
url: baseurl+='/controllers/ajax.php',
type: 'POST',
contentType: 'application/json; charset=utf-8',
data: {action : opt},
dataType: 'JSON',
success: function(data){
//self.setImages(data);
console.log(data)
},
error: function(){
console.log('NOPE');
}
});
}
PHP:
class ajax_controller {
function __construct(){
if(isset($_POST['action']) && !empty($_POST['action'])) {
$action = $_POST['action'];
switch($action) {
case 'Newborn' : $this->Newborn();
break;
}
}
}
/*
* Process Newborn Gallery request.
*/
public function Newborn(){
header('Content-type: application/json');
echo json_encode(array(
'images/gallery/albums/newborn/kylie/thumbnail/kylie-album.jpg',
'images/gallery/albums/newborn/payton/thumbnail/payton-1-thumbnail.png'
));
}
}
控制台/調試器/網絡面板都說我正在正確地與ajax控制器通信,但是,success方法的data
只返回null。
我是PHP的新手,任何建議都非常感謝。
謝謝,肯
UPDATE
我的調用仍然返回null,所以我想我會在這里粘貼我的標題。
Request URL:http://local.sunnyrose.dev/controllers/ajax.php
Request Method:POST
Status Code:200 OK
Request Headersview source
Accept:application/json, text/javascript, */*; q=0.01
Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.3
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8
Connection:keep-alive
Content-Length:14
Content-Type:application/json; charset=UTF-8
Host:local.sunnyrose.dev
Origin:http://local.sunnyrose.dev
Referer:http://local.sunnyrose.dev/
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/535.11 (KHTML,
like Gecko) Chrome/17.0.963.56 Safari/535.11
X-Requested-With:XMLHttpRequest
Request Payload
action=Newborn
Response Headersview source
Connection:Keep-Alive
Content-Length:0
Content-Type:application/json
Date:Mon, 05 Mar 2012 17:49:53 GMT
Keep-Alive:timeout=5, max=92
Server:Apache/2.2.14 (Unix) DAV/2 mod_ssl/2.2.14 OpenSSL/0.9.8l PHP/5.3.1 mod_perl/2.0.4
Perl/v5.10.1
X-Powered-By:PHP/5.3.1
在構造函數結束時回顯。我認為你不會在控制器中回顯任何東西,所以ajax響應為空。 使用whioch框架?
我認為這不會產生有效的JSON。 如果你想要一個帶數字鍵的實際數組,那么在PHP數組中使用數字鍵:
echo json_encode(array(
1 => 'images/gallery/albums/newborn/kylie/thumbnail/kylie-album.jpg',
2 => 'images/gallery/albums/newborn/payton/thumbnail/payton-1-thumbnail.png'
));
要么
echo json_encode(array(
'images/gallery/albums/newborn/kylie/thumbnail/kylie-album.jpg',
'images/gallery/albums/newborn/payton/thumbnail/payton-1-thumbnail.png'
));
這將輸出以下js:
[
'images/gallery/albums/newborn/kylie/thumbnail/kylie-album.jpg',
'images/gallery/albums/newborn/payton/thumbnail/payton-1-thumbnail.png'
]
在你的/controllers/ajax.php文件中你運行你的功能嗎?
class ajax_controller {
function __construct(){
if(isset($_POST['action']) && !empty($_POST['action'])) {
$action = $_POST['action'];
switch($action) {
case 'Newborn' : return $this->Newborn();
break;
}
}
}
/*
* Process Newborn Gallery request.
*/
public function Newborn(){
header('Content-type: application/json');
return json_encode(array(
'images/gallery/albums/newborn/kylie/thumbnail/kylie-album.jpg',
'images/gallery/albums/newborn/payton/thumbnail/payton-1-thumbnail.png'
));
}
}
$controller = new ajax_controller;
echo $controller->__construct();
你正在使用url
一部分
url: '/controllers/ajax.php',
嘗試使用你的完整url
喜歡
var baseurl = 'http://www.example.com';
url: baseurl+'/controllers/ajax.php',
編輯
嘗試改變
header('Content-type: application/json')
至
header('Content-type: text/json');
要么
header('Content-type: text/plain');
https://stackoverflow.com/questions/267546/correct-http-header-for-json-file
我終於挖了標題,意識到問題就在於此。 ajax()
向我的PHP腳本發送一個空數組。
修復是擺脫contentType: 'application/json; charset=utf-8
contentType: 'application/json; charset=utf-8
,以及PHP腳本中的header()
函數。
它現在發送和接收數據就好了。 ( 去閱讀配置文檔 )
感謝所有的幫助 - 我的腳本會因為其他原因因為你的許多答案而被打破:)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.