我尝试将有关客户端的评论存储在管理面板中。
在Laravel 5.5中发送Ajax请求时收到错误405(不允许使用方法)
当我使用不带ajax的html表单发送数据时,一切工作正常。
Ajax:
$.ajaxSetup({
headers: {
"X-CSRF-TOKEN": $("meta[name='csrf-token']").attr("content")
}
});
$(".save-comment").click(function(e) {
e.preventDefault();
var id = $("#hidden-id").val();
var type = $("#slect-comment-type option:selected").val();
var text = $("#comment").val();
console.log(id);
console.log(type);
console.log(text);
$.post("storeComment", { id: id, type: type, text: text })
.done(function(data) {
console.log(data);
alert("Success");
$("#add-comment").css("display", "none");
})
.fail(function() {
alert("Fail");
});
});
我在控制台中看到的{{id:id,type:type,text:text}中传递了正确的数据。
路线:
Route::post('/comments/store' , 'CommentController@store')->name('storeComment');
CommentController:
public function store(Request $request) {
$id = $request->input('id');
$type = $request->input('type');
$text = $request->input('text');
$comment = new Comment();
$comment->client_id = $id;
$comment->type = $type;
$comment->text = $text;
$comment->created_by = $request->user()->first_name.' '.$request->user()->last_name;
$comment->save();
return response()->json(['success'=>'Success']);
}
HTML:
<form class="form-horizontal" method="POST" action="{{route ('storeComment')}}">
{{ csrf_field() }}
<input id="hidden-id" type="hidden" name="id" value="{{$id}}">
<div class="form-group">
<label class="col-md-4 control-label" for="commentSelect">Comment type</label>
<div class="col-md-6">
<select id="slect-comment-type" name="type" class="form-control">
<option value="Call">Call</option>
<option value="Mail">Mail</option>
</select>
</div>
</div>
<div class="form-group{{ $errors->has('comment') ? ' has-error' : '' }}">
<label for="comment" class="col-md-4 control-label">Comment</label>
<div class="col-md-6">
<textarea name="text" class="form-control" rows="5" id="comment"></textarea>
@if ($errors->has('comment'))
<span class="help-block">
<strong>{{ $errors->first('comment') }}</strong>
</span>
@endif
</div>
</div>
<div class="form-group">
<div class="col-md-6 col-md-offset-4">
<button type="submit" class="btn btn-primary btn-sm save-comment">Save</button>
</div>
</div>
</form>
答案 0 :(得分:1)
用“ /”解决
let myView = UIView(frame: .init(x: 0, y: 0, width: 200, height: 150))
view.addSubview(myView)
myView.center = view.center
// Start and finish point
let startPoint = CGPoint(x: myView.bounds.minX, y: myView.bounds.midY)
let finishPoint = CGPoint(x: myView.bounds.maxX, y: myView.bounds.midY)
// Path
let path = UIBezierPath()
path.move(to: startPoint)
path.addLine(to: finishPoint)
// Gradient Mask
let gradientMask = CAShapeLayer()
let lineHeight = myView.frame.height
gradientMask.fillColor = UIColor.clear.cgColor
gradientMask.strokeColor = UIColor.black.cgColor
gradientMask.lineWidth = lineHeight
gradientMask.frame = myView.bounds
gradientMask.path = path.cgPath
// Gradient Layer
let gradientLayer = CAGradientLayer()
gradientLayer.startPoint = CGPoint(x: 0.0, y: 0.5)
gradientLayer.endPoint = CGPoint(x: 1.0, y: 0.5)
// make sure to use .cgColor
gradientLayer.colors = [UIColor.red.cgColor, UIColor.green.cgColor]
gradientLayer.frame = myView.bounds
gradientLayer.mask = gradientMask
myView.layer.addSublayer(gradientLayer)
// Corner radius
myView.layer.cornerRadius = 10
myView.clipsToBounds = true
答案 1 :(得分:0)
$.post("comments/store", { id: id, type: type, text: text })
.done(function(data) {
console.log(data);
alert("Success");
$("#add-comment").css("display", "none");
})
.fail(function() {
alert("Fail");
});
});
答案 2 :(得分:0)
将store_comment_form id赋予表单,然后:
$('#store_comment_form').on('submit',(function(e){
e.preventDefault();
var data = new FormData(jQuery('#store_comment_form')[0]);
$.ajax({
url:"{{url('/comments/store')}}",
type:'POST',
data: data,
success:function(response){
}
});
}));