Javascript 从ajax调用返回的php为空
我用ajax调用php函数,但php函数的返回值为空 这是我的ajax调用:Javascript 从ajax调用返回的php为空,javascript,php,jquery,ajax,return,Javascript,Php,Jquery,Ajax,Return,我用ajax调用php函数,但php函数的返回值为空 这是我的ajax调用: showEvent : function(eventId){ $.ajax({ type: "POST", url: "routing.php?page=Events&action=showEvent", data : {event_id :eventId}, dataType : "text
showEvent : function(eventId){
$.ajax({
type: "POST",
url: "routing.php?page=Events&action=showEvent",
data : {event_id :eventId},
dataType : "text",
success: function(data){
console.log(data);
$('#showEvent').modal("show");
$('#showClickedEvent').children("tr").val("test");
},
error: function(xhr,status,e){
console.log(xhr);
console.log(status);
console.log(e);
}
});
}
和我的php函数:
function call($page,$action){
require_once("Controller/".$page."Controller.php");
$request = array_merge($_GET,$_POST);
switch($page)
{
case "Index":
switch($action)
{
case "home":
return IndexController::home();
break;
case "login":
return IndexController::login();
break;
}
case "Events":
switch($action)
{
case "createEvent":
return EventsController::createEvent();
break;
case "setEvent":
EventsService::setEvent($request["homepartyName"],$request["date"],$request["musicDirection"],
$request["ageFrom"],$request["ageTo"],$request["maxPersons"],$request["country"],$request["city"],$_COOKIE["facebook_id"]);
break;
case "showEvents":
break;
case "myEvents":
return EventsController::myEvents();
break;
case "showEvent":
return EventsService::getEvent($request["event_id"]);
break;
}
case "User":
switch($action)
{
case "createUser":
UserService::setUser($request["facebookData"]);
break;
case "logoutUser":
UserController::logoutUser();
break;
}
}
}
my Eventservice正在调用此方法:
public static function getEvent($id){
$db = parent::getInstance();
$stmt = "SELECT * FROM events WHERE id = ".$id."";
$event = $db->query($stmt)->fetch_all(MYSQLI_ASSOC) or die($db->error);
header('Content-Type: application/json; charset=utf8');
return json_encode($event);
}
我不知道为什么返回为空:///p>请在ajax函数中更改数据类型:'JSON',因为我们试图从ajax响应而不是文本中获取JSON
也可以使用退出(json_encode($event))而不是返回json_encode($event)来获得正确的json响应。PHP需要回显json。“id”字段是整数吗?尝试回显sql语句并直接在数据库中运行,以确保获得预期结果。您需要将返回值作为字符串回显到responseText。简单地调用
return
不会有任何效果。sir在url:“routing.php?page=Events&action=showEvent”中,php中名为call($page,$action)的函数不是call函数调用($page,$action)调用来自何处?