laravel 5.1请求::ajax返回false
您好,我对Laravel4.2和Laravel5.1使用了相同的脚本,问题是Laravel 4.2工作得很好,但在Laravel 5.1上,我不明白为什么会返回坏结果 问题是为什么我得到了Laravel5.1的laravel 5.1请求::ajax返回false,ajax,laravel,laravel-4,laravel-5,laravel-5.1,Ajax,Laravel,Laravel 4,Laravel 5,Laravel 5.1,您好,我对Laravel4.2和Laravel5.1使用了相同的脚本,问题是Laravel 4.2工作得很好,但在Laravel 5.1上,我不明白为什么会返回坏结果 问题是为什么我得到了Laravel5.1的$request->ajax()false routes.php Route::post('/upload-image', [ 'as' => 'upload-image-post', 'uses' => 'PageController@profileImage
$request->ajax()
false
routes.php
Route::post('/upload-image', [
'as' => 'upload-image-post',
'uses' => 'PageController@profileImage'
]);
PageController.php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
class PageController extends Controller
{
public function profileImage(Request $request)
{
//here is problem
//result good: (Laravel 4.2 shows true)
//result bad: (Laravel 5.1 shows false)
var_dump($request->ajax());
}
}
upload-image.blade.php(js)
jQuery(文档).ready(函数($){
$(“#上载_图像”)。单击(函数(事件){
$('#image')。触发器('click');
});
$('#image')。更改(函数(事件){
/*对事件采取行动*/
$('#imageform')。提交();
});
$('#imageform').ajaxForm({
beforeSubmit:函数(){
},
成功:功能(msg){
},
错误:功能(请求、状态、错误){
},
完成:函数(xhr){
如果(xhr.status!=401){
$('#image').val('');
结果=xhr.responseText;
结果=$.parseJSON(结果);
if(!$.isEmptyObject(result.file\u base\u url)){
var img=新图像();
img.onload=函数(){
$('#register_profile_photo').attr('src',img.src);
$('#register_profile_photo').attr('alt',result.image_alt);
//spinner.stop();
$(“#上传图像错误”).text(“”);
}
img.src=result.file\u base\u url;
}否则{
$(“#上载图像错误”).text(result.image[0]);
}
}
}
});
});
upload-image.blade.php(html)
{!!csrf_field()!!}
另外,如果您测试laravel 4.2,则此代码需要从“{!!..!!}”更改为“{{..}}”我不认为此问题是由laravel版本引起的。Laravel源代码显示,
ajax()
调用被传播到。5年前,这个消息来源发生了很大变化
您应该跟踪在这两种情况下是否向您的应用程序发送了
X-request-With
头。您还可以将断点设置为Symfony\Component\HttpFoundation\Request::isXmlHttpRequest()
,并查看标题中的内容。尝试$Request->wantsJson()
相反,它依赖于标准的接受
标题,而不是非标准的X-Request-With
标题。这可能与控制器的其他部分工作正常吗?其他方法是简单方法,而不是简单方法ajax@watcher我想你不明白这个问题。。我没有收到ajax请求,在按下图像编辑后,我在路由中得到了重定向,但没有收到ajax请求…@J.Kol,试试watcher的方法。他是对的,他已经理解了这个问题。
<script>
jQuery(document).ready(function($) {
$('#upload_image').click(function(event) {
$('#image').trigger('click');
});
$('#image').change(function(event) {
/* Act on the event */
$('#imageform').submit();
});
$('#imageform').ajaxForm({
beforeSubmit: function() {
},
success: function(msg) {
},
error: function(request, status, error) {
},
complete: function(xhr) {
if(xhr.status != 401) {
$('#image').val('');
result = xhr.responseText;
result = $.parseJSON(result);
if( !$.isEmptyObject(result.file_base_url) ) {
var img = new Image();
img.onload = function() {
$('#register_profile_photo').attr('src', img.src);
$('#register_profile_photo').attr('alt', result.image_alt);
//spinner.stop();
$('#upload-image-error').text('');
}
img.src = result.file_base_url;
} else {
$('#upload-image-error').text(result.image[0]);
}
}
}
});
});
<form enctype="multipart/form-data" name='imageform' role="form" id="imageform" method="post" action="{!! route('upload-image-post') !!}">
{!! csrf_field() !!}
<div style="display:none;">
<input type="file" name="image" id="image" placeholder="Please choose your image" >
</div>
</form>
<div class="profile-img">
<img style="float: right" id="register_profile_photo" src="default.png" alt="Default image">
<div style="float: right" class="img-edit"><a href="javascript:void(0);" id="upload_image">Edit picture</a></div>
</div>