从laravel中的控制器返回JSON响应
Returning JSON response from controller in laravel
我是Javascript新手。我想对表单进行ajax更新。一切正常。更新完成后,数据库中的值就更新了。当我试图在视图中更新它时,问题就出现了。这是我的控制器
public function updatePost(Request $request)
{
$post = $request->all();
$val = 'Validator::make($request->all(), [
'client_id' => 'required',
'client_name' => 'required',
'client_business' => 'required',
'client_ref' => 'required',
'gmail_mail' => 'required',
'gmail_pass' => 'required',
'client_dob' => 'required',
'client_addr' => 'required',
'client_no1' => 'required',
'client_ref' => 'required',
'domain_name' => 'required',
'domain_p_date' => 'required',
'domain_reg' => 'required',
'domain_ex_date' => 'required',
'domain_acc_email' => 'required',
'domain_acc_pass' => 'required',
'domain_id' => 'required',
]);
if ($val->fails()) {
return redirect()->back()->withErrors($val->errors());
}
else {
$data = array(
'client_id' => $post['client_id'],
'client_name' => $post['client_name'],
'client_business' => $post['client_business'],
'client_ref' => $post['client_ref'],
'gmail_mail' => $post['gmail_mail'],
'gmail_pass'=> $post['gmail_pass'],
'client_dob'=> $post['client_dob'],
'client_addr'=> $post['client_addr'],
'client_no1'=> $post['client_no1'],
'domain_name' => $post['domain_name'],
'domain_p_date' => $post['domain_p_date'],
'domain_reg' => $post['domain_reg'],
'domain_ex_date' => $post['domain_ex_date'],
'domain_acc_email' => $post['domain_acc_email'],
'domain_acc_pass' => $post['domain_acc_pass'],
'domain_id' => $post['domain_id'],
);
//var_dump($data);
$update_data = domain_details::where('domain_id', $post['domain_id'])
->update($data);
if ($update_data) {
$new_data = domain_details::where('domain_id',$post['domain_id'])->get();
return response()->json( ['client_id'=> 'johncena'],200);
}
else
{
return redirect()->back()->withErrors($val->errors());
}
}
这是我从表单和更新中收到的数据。数据库上的更新工作正常。这就是问题发生的地方
if ($update_data) {
$new_data = domain_details::where('domain_id',$post['domain_id'])->get();
return response()->json( ['client_id'=> $new_data->client_id],200);
json(['client_id'=> $new_data->client_id],200);如果我只是传递一个字符串,它的工作很好像json(['client_id'=> 'John Doe'],200);。
另一个问题是当我看到控制台。如果我尝试在不改变任何东西的情况下更新表单,它会抛出500个内部服务器错误。下面是处理控制器响应的Javascript代码。有时字符串被更改为约翰Doe,大多数时候我得到500内部服务器错误。
$('#update-modal').on('click',function(){
$.ajax({
type : "get",
url : updateURL,
data : { client_id : $('#client_id').val(),
client_name : $('#client_name').val(),
client_business : $('#client_business').val(),
client_ref : $('#client_ref').val(),
gmail_mail : $('#gmail_mail').val(),
gmail_pass : $('#gmail_pass').val(),
client_dob : $('#client_dob').val(),
client_addr : $('#client_addr').val(),
client_no1 : $('#client_no1').val(),
domain_name : $('#domain_name').val(),
domain_p_date : $('#domain_p_date').val(),
domain_reg : $('#domain_reg').val(),
domain_ex_date : $('#domain_ex_date').val(),
domain_acc_email : $('#domain_acc_email').val(),
domain_acc_pass : $('#domain_acc_pass').val(),
domain_id : domain_id,
_token : token
}
})
.done(function(msg){
$(client_id_element).text(msg['client_id']);
});
});
我知道我错过了什么。我不知道那是什么。提前感谢
我认为Eloquent get()
返回一个集合,而不是某个对象。看起来您只想返回一个具有唯一domain_id
的对象。尝试使用
$new_data = domain_details::where('domain_id',$post['domain_id'])->first();
。
相关文章:
- Jquery:对返回JSON数据的php脚本的Ajax调用
- 返回JSON中的下标
- jQuery Map 从多个源返回 JSON 对象
- 如何在 Node.js 中返回 JSON 后停止代码流
- AngularJS:表单提交没有返回JSON中的值
- 使用ajax发送数据,返回json
- 从我的解析函数返回json
- 如何从脚本页面返回json数据
- 有些人有任何关于如何使用Modified Java Script Value处理返回Json(url)的示例
- 如何在html页面中从ajax返回json数据
- Ajax调用在本地返回json,但在服务器上运行时返回xml
- ajax调用返回JSON ParseError即使认为JSON看起来是正确的
- 调用.ajax方法得到未定义的json结果,返回json格式列表<字符串>
- 如何通过C#返回JSON并用JavaScript进行解析
- 使用 AJAX 返回 json 数组
- 从 AJAX 命令返回 JSON 格式的响应
- 如何通过node中的请求返回JSON.js(从javascript)
- Node.js API 在终端中返回 JSON,但不在浏览器中返回 JSON
- 如何在支柱 2 中返回 JSON 数组
- Rhino:从 Java 中返回 JSON