Symfony \组件\ HttpKernel \ Exception \ MethodNotAllowedHttpException没有消息Laravel 5.8和Ajax [英] Symfony \ Component \ HttpKernel \ Exception \ MethodNotAllowedHttpException No message Laravel 5.8 and Ajax
问题描述
我正在尝试在潜在客户系统中存储多封电子邮件.我正在使用Laravel 5.8构建系统.我正在尝试使用AJAX请求发送电子邮件.而不是使用重定向返回页面,而是保持相同的模式.
I'm trying to store multiple emails in a prospect system. I'm building the system with Laravel 5.8. I'm trying to put the emails using the AJAX request. And not return in a page using redirect, but keep in the same modal.
这是用于过去只能存储一封电子邮件的系统.但是用户说需要将多个电子邮件存储到客户端.现在,我正在尝试创建一个功能,可以在潜在客户中添加多封电子邮件,并且当用户发送投标时,将显示该潜在客户中存储的所有电子邮件.
This is for a system that in the past, can only store one email. But a user says that need to store more than one email to the client. Now i'm trying to create a functionality that in a prospect, you can add multiple emails and when the user will send a proposal, will show all emails stored in that prospect.
我需要使用AJAX请求来执行此操作.现在,我尝试使用方法"POST"继续我的功能,将其放入路线,视图和AJAX请求中.
I'm need to use the AJAX request to do it. Now I try to use the method "POST" to proceed with my functionality, i put it in my route, my view and my AJAX request.
这是我的路线
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});
});
现在,我一直在尝试注册新电子邮件,显示以下消息:
Now always I'm trying to register a new email, show this message:
Symfony \组件\ HttpKernel \ Exception \ MethodNotAllowedHttpException
Symfony \ Component \ HttpKernel \ Exception \ MethodNotAllowedHttpException
没有消息
在控制台中显示此错误.
In console show this error.
POST http://127.0.0.1:8000/prospect 405(不允许使用方法)
POST http://127.0.0.1:8000/prospect 405 (Method Not Allowed)
将数据存储在我的数据库中但显示出来的奇怪对象.显示此消息会怎样?
The strange that store the data in my database, but show it. What happens to show this message?
我改为PUT方法.起初它可以工作,但是会显示令牌和我存储在URL中的内容.这不是我想要的.
I change to PUT method. At first it works, but show the token and the things that i store in URL. It's not what i want.
推荐答案
您的路由已定义为prospect/emails/save
,但是您要发布到prospect
,显然它不作为POST路由存在.
Your route is defined as prospect/emails/save
, but you're POSTing to prospect
, which apparently doesn't exist as a POST route.
发布到正确的路线,它应该可以正常工作.将action
添加到<form>
应该可以解决问题:
Post to the correct route and it should work fine. Adding an action
to your <form>
should do the trick:
<form action="{{ route('prospectEmails.store') }}" ...>
这篇关于Symfony \组件\ HttpKernel \ Exception \ MethodNotAllowedHttpException没有消息Laravel 5.8和Ajax的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!