2017-01-09 137 views
0

当用户选择客户名称(Laravel应用程序)时,我需要显示客户地址。以下是我的jQuery代码:NetworkError:500内部服务器错误 - Laravel Ajax

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script> 
<script type="text/javascript"> 
    $(function() { 
     $('#customername').change(function() { 
      var choice = jQuery(this).val(); 
      $.ajax({ 
       url:'{{URL('home/getcustomernameaddress')}}', 
       type:'POST', 
       data : {'id' : choice,'_token': '{{ csrf_token() }}'}, 
       success : function(response) { 
        alert(response); 
        var json = $.parseJSON(response); 
        $('span#message').html(json.address123); 
        $('span#message1').html(json.address456); 
       } 
      }); 
     }); 
    }) 
</script> 

家/ getcustomernameaddress

$data = $_POST; 
$getcustomerdatas=DB::table('customers')->where('id', '=', $data["id"])->first(); 
echo json_encode(array('address123'=>$getcustomerdatas["address1"],'address456'=>$getcustomerdatas["address2"])); 

显示以下错误:

"NetworkError: 500 Internal Server Error - http://localhost/invoice/home/getcustomernameaddress "

+0

告诉你laravel错误堆栈,你的route.php –

+0

路线::职位(” getcustomernameaddress', '发票\ InvoiceController @ getcustomernameaddress'); @Kris Roofe – Karthik

+0

尝试通过json响应:return response() - > json($ response); –

回答

1

变化的Json编码为以下

json_encode(array('address123'=>$getcustomerdatas->address1,'address456'=>$getcustomerdatas->address2)); 
-1

尝试添加斜线/

url:'{{URL("/home/getcustomernameaddress")}}',

0

替换下面的代码:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js">   </script> 
<script type="text/javascript"> 
$(function() { 
    $('#customername').change(function() { 
     var choice = jQuery(this).val(); 
     $.ajax({ 
      url:'{{URL('home/getcustomernameaddress')}}', 
      type:'POST', 
      dataType:'json', 
      data : {'id' : choice,'_token': '{{ csrf_token() }}'}, 
      success : function(response) { 
       alert(response); 
       var json = $.parseJSON(response); 
       $('span#message').html(json.address123); 
       $('span#message1').html(json.address456); 
      } 
     }); 
    }); 
}) 

相关问题