【发布时间】:2020-01-01 22:52:39
【问题描述】:
我正在尝试在潜在客户系统中存储多封电子邮件。我正在使用 Laravel 5.8 构建系统。我正在尝试使用 AJAX 请求发送电子邮件。并且不使用重定向返回页面,而是保持相同的模式。
这适用于过去只能存储一封电子邮件的系统。但一位用户表示,需要向客户存储多封电子邮件。现在我正在尝试在潜在客户中创建一个功能,您可以添加多封电子邮件,当用户发送提案时,将显示存储在该潜在客户中的所有电子邮件。
我需要使用 AJAX 请求来执行此操作。现在我尝试使用“POST”方法来继续我的功能,我把它放在我的路线、我的视图和我的 AJAX 请求中。
这是我的路线
Route::POST('prospect/emails/save','ProspectEmailsController@store')->name('prospectEmails.store');
这是我的看法
<form id="emailModalProspect" method="POST">
@csrf
<input hidden name="prospect_id" id="prospect_id" type="text">
<div class="form-group mb-3">
<label class="form-control-label" for="prospect-email-name">{{ __('Nome') }}</label>
<div class="input-group input-group-alternative">
<div class="input-group-prepend">
<span class="input-group-text"><i class="ni ni-email-83"></i></span>
</div>
<input class="form-control" id="prospect-email-name" name="name" placeholder="Put the email owner" type="text">
</div>
</div>
<div class="form-group mb-3">
<label class="form-control-label" for="prospect-email-email">{{ __('Email') }}</label>
<div class="input-group input-group-alternative">
<div class="input-group-prepend">
<span class="input-group-text"><i class="ni ni-email-83"></i></span>
</div>
<input class="form-control" id="prospect-email-email" name="email" placeholder="Put the email" type="email">
</div>
</div>
<div class="text-center">
<button type="submit" id="save-email" class="btn btn-primary my-4 store-email">Store</button>
</div>
</form>
这是我的控制器
public function store(Request $request){
$prospect_emails = ProspectEmails::where(['prospect_id'=>$request->prospect_id])->get();
ProspectEmails::create(array_merge($request->all(), ['company_id' => Auth::User()->company_id], ['prospect_id'=>$request->prospect_id], ['tag'=>false]));
$p_email = ProspectEmails::where('prospect_id',$request->prospect_id)->get()->count();
$update_at = Carbon\Carbon::now();
Prospect::where('id', $request->prospect_id)->update(['updated_at' => $update_at, 'prospect_emails'=> $p_email]);
return response()->json();
}
这是我的 Ajax 请求
$('.open-email-modal').on('click', function(e) {
e.preventDefault();
let p = JSON.parse($(this).attr('data-entity')); //the id of the prospect that i want to insert the emails
let modal = $('#emailModal');
let form = $('#emailModalProspect');
$('#prospect-email-name').val(p.name);
$('#prospect_id').val(p.id).change();
form.submit(function(e){
if(p.id) {
$.ajax({
url: "{{route('prospectEmails.store')}}",
type: "POST",
data : form.serialize() ,
dataType: "json",
success:function(data) {
if(data){
console.log(data); // here, in console, show a empty array like it "[]".
}
}
});
}
});
modal.modal({show : true});
});
现在我总是在尝试注册新电子邮件,请显示此消息:
Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException
没有消息
在控制台中显示此错误。
POST http://127.0.0.1:8000/prospect405(方法不允许)
奇怪的是把数据存储在我的数据库里,但是显示出来。显示此消息会发生什么情况?
我改为 PUT 方法。起初它有效,但显示令牌和我存储在 URL 中的东西。这不是我想要的。
【问题讨论】:
-
您能分享您的要求吗?
-
打开您的开发控制台以查看控制台中的任何错误
-
我将在我的问题中编辑结果。但在 console.log(data) 中显示一个空数组“[]”并显示结果:prospect:1 POST 127.0.0.1:8000/prospect405 (Method Not Allowed)
-
您发送的标头是什么,您发送的是 csrf 令牌吗?
-
一开始,我想。
标签: php laravel laravel-5.8