dongqing7789 2019-08-08 10:26
浏览 83

ajax无法在Laravel中显示找不到页面

I want to send OTP with Laravel and ajax but when i'm calling ajax it shows error page not found...

HTML:

`

                    <div id="first_step">
                        <div class="col-md-4">
                            <input value="+91" type="text" placeholder="+1" id="country_code" name="country_code" />
                        </div> 

                        <div class="col-md-8">
                            <input type="text" autofocus id="phone_number" class="form-control" placeholder="Enter Phone Number" name="phone_number" value="{{ old('phone_number') }}" />
                        </div>
                         <div class="col-md-4">

                        </div> 
                         <div class="col-md-8 " id="otp_input">
                            <input type="text" autofocus id="user_otp" class="form-control" placeholder="Enter OTP" name="otp" id="result" value="{{ old('phone_number') }}" /> 
                        </div>
                        <div class="col-md-12" style="padding-bottom: 10px;" id="mobile_verfication1"> </div>
                           <div class="col-md-12" style="padding-bottom: 10px;" id="mobile_verfication">
                            <input type="button" class="log-teal-btn small" id="send_otp_button" value="Verify Phone Number"/>
                        </div>



                    </div>

`

script:

$('#send_otp_button').on('click', function(e) {
   e.preventDefault(); 
   var phone_number = $('#phone_number').val();
   alert(phone_number);
   var host = "{{URL::to('/')}}";
   alert(host);
 $.ajax({
       type: "POST",
       url: host+"/send_otp",
       data: {name:phone_number},
       success: function( msg ) {
           alert( msg );
       },
       error: function (request, status, error) {
    alert(request.responseText);
}
   }); });

Route in web.php:

Route::post('/send_otp', 'AccountAuth\RegisterController@send_otp_function');

//Route::post('/send_otp', 'AccountAuth\RegisterController@send_otp_function'); also try this

Controllers:

public function send_otp_function(Request $request)
{
 $response = array(
        'status' => 'success',
        'msg' => 'Setting created successfully',
    );
    return Response::json($response);
}
  • 写回答

3条回答 默认 最新

  • doutuan4361 2019-08-08 11:01
    关注

    Welcome to SO. As far as I understand and according to jQuery.ajax documentation you have forgotten to set datatype='json' in your ajax request. Your request should be:

    $.ajax({
      type: "POST",
      url: host + "/send_otp",
        datatype: 'json',
      data: {
        name: phone_number
      },
      success: function(msg) {
        alert(msg);
      },
      error: function(request, status, error) {
        alert(request.responseText);
      }
    });
    
    评论

报告相同问题?