我是Laravel的新手,并且构建了简单的AJAX函数,在控制台中看到“ hello”,但实际上并没有在Chrome中发出任何警报。有谁知道原因。感谢您的关注。
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$(".myTestLink").click(function(){
$.ajax({
method: 'post',
dataType: 'json',
beforeSend: function (request) {
return request.setRequestHeader('X-CSRF-Token', $("meta[name='csrf-token']").attr('content'));
},
url: 'insert-ajax',
success: function (data)
{
alert(data);
}
});
});
});
</script>
routes / web.php
<?php
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/', function () {
return view('index');
});
Route::post('/insert-ajax', 'myTestController@testingsomething');
myTestController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class myTestController extends Controller
{
public function testingsomething()
{
return "hello";
}
}
答案 0 :(得分:0)
进行ajax调用时,将dataType: 'json',
切换为dataType: 'text',
。 Alert无法处理json响应。