我在向电子邮件发送ajax时遇到了一些问题。 我需要输出错误json,如果我将字段留空或不正确则返回"错误:不可处理的实体" 我遇到的更大问题是它没有发送电子邮件并返回500错误,但如果我要稍微配置它并且只发送名称就可以了。
Mail.blade.php 这是一种简单的格式
<h1>{{ $name }}</h1>
<h1>{{ $message }}</h1>
<h1>{{ $email }}</h1>
EmailController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Mail;
class EmailController extends Controller {
public function basic_email(Request $request){
$this->validate($request, [ 'name' => 'required', 'email' => 'required|email', 'message' => 'required' ]);
ContactUS::create($request->all());
Mail::send('emails.mail',
array(
'name' => $request->get('name'),
'email' => $request->get('email'),
'message' => $request->get('message')
), function($message)
{
$message->from('test@test.com');
$message->to('test@test.com', 'Admin')->subject('Feedback');
});
return response()->json('success', 'Thanks for contacting us!');
}
}
request.php
<?php
class ContactFormRequest extends Request
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'name' => 'required',
'email' => 'required|email',
'message' => 'required',
];
}
}
Contact.Blade.PHP
<html>
<head>
<title>Test</title>
<script src = "https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js">
</script>
<meta name="csrf-token" content="{{ csrf_token() }}" />
<script>
function getMessage(){
var CSRF_TOKEN = $('meta[name="csrf-token"]').attr('content');
var name = $('.namebox').val();
var email = $('.namebox2').val();
var message = $('.namebox3').val();
$.ajax({
type:'post',
url:'/sendbasicemail',
data: {_token: CSRF_TOKEN, name: name, email: email, message: message},
dataType: 'JSON',
success:function(data){
alert("DONE");
console.log(data);
},
error: function(XMLHttpRequest, textStatus, errorThrown, request) {
alert("Status: " + textStatus); alert("Error: " + errorThrown);
console.log(name, email, message);
}
});
}
</script>
</head>
<ul>
@foreach($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
{!! Form::open() !!}
<div class="form-group {{ $errors->has('name') ? 'has-error' : '' }}">
{!! Form::label('Name:') !!}
{!! Form::text('name', '',['class'=>'namebox', 'placeholder'=>'Enter Name']) !!}
<span class="text-danger">{{ $errors->first('name') }}</span>
</div>
<div class="form-group {{ $errors->has('email') ? 'has-error' : '' }}">
{!! Form::label('Email:') !!}
{!! Form::text('email', '',['class'=>'namebox2', 'placeholder'=>'Enter Email']) !!}
<span class="text-danger">{{ $errors->first('email') }}</span>
</div>
<div class="form-group {{ $errors->has('message') ? 'has-error' : '' }}">
{!! Form::label('Message:') !!}
{!! Form::textarea('message', '',['class'=>'namebox3', 'placeholder'=>'Enter Message']) !!}
<span class="text-danger">{{ $errors->first('message') }}</span>
</div>
{!! Form::close() !!}
<body>
<div id = 'msg'>This message will be replaced using Ajax.
Click the button to replace the message.</div>
<?php
echo Form::button('Replace',['onClick'=>'getMessage()']);
?>
</body>
</html>
web.php
Route::get('/ajax',function(){
return view('pages.contact');
});
Route::post('/sendbasicemail','EmailController@basic_email');
另一方面,我在外面尝试了一些,所以你可能会注意到一些差异
答案 0 :(得分:0)
您的代码有点乱我建议检查您的contact.blade.php
您的html标签序列不正确,
我在body标签外面看到了form标签,并在表单标签外面提交了按钮。
我试图重新组织您的视图文件:
<html>
<head>
<title>Test</title>
<script src = "https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js">
</script>
<meta name="csrf-token" content="{{ csrf_token() }}" />
<script>
function getMessage(){
var CSRF_TOKEN = $('meta[name="csrf-token"]').attr('content');
var name = $('.namebox').val();
var email = $('.namebox2').val();
var message = $('.namebox3').val();
$.ajax({
type:'post',
url:'/sendbasicemail',
data: {_token: CSRF_TOKEN, name: name, email: email, message: message},
dataType: 'JSON',
success:function(data){
alert("DONE");
console.log(data);
},
error: function(XMLHttpRequest, textStatus, errorThrown, request) {
alert("Status: " + textStatus); alert("Error: " + errorThrown);
console.log(name, email, message);
}
});
}
</script>
</head>
<body>
<ul>
@foreach($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
{!! Form::open() !!}
<div class="form-group {{ $errors->has('name') ? 'has-error' : '' }}">
{!! Form::label('Name:') !!}
{!! Form::text('name', '',['class'=>'namebox', 'placeholder'=>'Enter Name']) !!}
<span class="text-danger">{{ $errors->first('name') }}</span>
</div>
<div class="form-group {{ $errors->has('email') ? 'has-error' : '' }}">
{!! Form::label('Email:') !!}
{!! Form::text('email', '',['class'=>'namebox2', 'placeholder'=>'Enter Email']) !!}
<span class="text-danger">{{ $errors->first('email') }}</span>
</div>
<div class="form-group {{ $errors->has('message') ? 'has-error' : '' }}">
{!! Form::label('Message:') !!}
{!! Form::textarea('message', '',['class'=>'namebox3', 'placeholder'=>'Enter Message']) !!}
<span class="text-danger">{{ $errors->first('message') }}</span>
</div>
<div id = 'msg'>This message will be replaced using Ajax.
Click the button to replace the message.</div>
<?php
echo Form::button('Replace',['onClick'=>'getMessage()']);
?>
{!! Form::close() !!}
</body>
</html>
然而,“错误:不可处理的实体”是一个错误,如果您在Ajax请求中传递数据而未对其进行序列化或者您的数据未在服务器端验证,则通常会导致错误
答案 1 :(得分:0)
问题排序。
在发布带有特殊字符的对象时,必须更改控制器变量。