【发布时间】:2017-05-17 23:27:57
【问题描述】:
我有一个 ajax 调用的问题。我需要获取数据的网址是:
localhost/public/getCode?course_id=1&task_id=1
我的 ajax 调用是:
function getCode() {
$.ajax({
type: "GET",
dataType: 'json',
url: "{{action('CodeEditorController@getCode',['course_id'=>$course,'task_id'=>$maintask])}}",
success: function (data) {
console.log(data);
}
});
}
但是返回的数据是空的。
编辑: 获取代码函数:
public function getCode(Request $request)
{
$code=Code::where('user_id',$user->id)->where('main_task_id',$request->input('task_id'))->first()->code;
$response = [
'data' => $code
];
return response()->json($response, 200);
}
我的 ajax 代码有什么问题?
谢谢!
【问题讨论】:
-
你可以在你的getCode方法中添加代码吗?
-
返回任何错误或只是空结果集?尝试在成功声明之后为 ajax 请求添加错误日志记录。
error: function(jqXHR, textStatus, errorThrown) { $content.fadeOut(200); console.log(JSON.stringify(jqXHR)); console.log("AJAX error: " + textStatus + ' : ' + errorThrown); -
@AndrewNolan 没有错误,只是空响应。使用邮递员它会返回数据,但使用我的 ajax 调用它不会。我认为这是我的 ajax 代码的问题
-
hmmm,也许可以尝试将您的参数添加为 ajax 调用中的数据部分,而不是附加到 url。
data: {'task_id' : $maintask, 'course_id' : $course}