laravel API,没有从数据库中获取数据
laravel API, not fetching data from DB
这是在生产端(在 unbtu Digital Ocean 服务器上)
- "laravel: 5.3"
- "php": "5.5.9"
路由路径:/route/web.php(不在中间件中)
Route::get('api/getsymbol-request','MemberTradesController@getSymbolRequest');
Route::get('api/getsymbol','MemberTradesController@getSymbol');
控制器:
public function getSymbolRequest(Request $request){
$symbol = DB::table("symbols")
->where("market_id", $request->market_id)
->pluck("symbol","id");
return response() -> json($symbol);
}
public function getSymbol(){
$symbol = DB::table("symbols")
->pluck("symbol","id");
return response() -> json($symbol);
}
http://yourtradelog.com/api/getsymbol-request?market_id=1 //这是不工作url我有问题为什么这不工作
http://yourtradelog.com/api/getsymbol //这是有效的url
Database tables
<script>
$(document).ready(function() {
$('#exchange').change(function(){
var exchangeID = $(this).val();
if(exchangeID){
$.ajax({
type:"GET",
url:"{{url('api/getsymbol-request/')}}?exchange_id="+exchangeID,
success:function(res){
if(res){
$("#market").empty();
$("#market").append('<option>Select</option>');
$.each(res,function(key,value){
$("#market").append('<option value="'+key+'">'+value+'</option>');
});
}else{
$("#market").empty();
}
}
});
}else{
$("#market").empty();
}
});
});
</script>
{!! Form::open(['method'=>'POST', 'action'=> 'MemberTradesController@store']) !!}
<div class="form-group col-sm-5">
{!! Form::label('market_id', 'Markets:') !!}
{!! Form::select('market_id', [''=>'Choose Options'] , null, ['class'=>'form-control', 'id'=>'market'])!!}
</div>
<div class="form-group col-sm-10">
{!! Form::label('symbol_id', 'Symbol:') !!}
{!! Form::select('symbol_id', [''=>'Choose Options'] , null, ['class'=>'symbol_id', 'id'=>'symbol','data-width'=>'60%', 'data-live-search'=>'true'] )!!}
</div>
<div class="form-group col-lg-4">
{!! Form::submit('Add trade', ['class'=>'btn btn-success btn-lg']) !!}
</div>
{!! Form::close() !!}
我更喜欢像这样使用路由参数(更多信息请参阅文档 here):
路线:
Route::get('api/getsymbol-request/{id}','MemberTradesController@getSymbolRequest');
Route::get('api/getsymbol','MemberTradesController@getSymbol');
控制器:
public function getSymbolRequest(Request $request, $id){
$symbol = DB::table("symbols")
->where("market_id", $id)
->get()
->pluck("symbol","id");
return response() -> json($symbol);
}
public function getSymbol(){
$symbol = DB::table("symbols")
->pluck("symbol","id");
return response() -> json($symbol);
}
Please note that the URL has changed to this format:
试试这个代码:
public function getSymbolRequest(Request $request)
{
$marketID = (int)$request->get('market_id')
$symbol = DB::table('symbols')
->where('market_id', $marketID)
->get(['symbol','id']);
return response()->json($symbol);
}
public function getSymbol()
{
$symbol = DB::table('symbols')
->get(['symbol','id']);
return response()->json($symbol);
}
这是在生产端(在 unbtu Digital Ocean 服务器上)
- "laravel: 5.3"
- "php": "5.5.9"
路由路径:/route/web.php(不在中间件中)
Route::get('api/getsymbol-request','MemberTradesController@getSymbolRequest');
Route::get('api/getsymbol','MemberTradesController@getSymbol');
控制器:
public function getSymbolRequest(Request $request){
$symbol = DB::table("symbols")
->where("market_id", $request->market_id)
->pluck("symbol","id");
return response() -> json($symbol);
}
public function getSymbol(){
$symbol = DB::table("symbols")
->pluck("symbol","id");
return response() -> json($symbol);
}
http://yourtradelog.com/api/getsymbol-request?market_id=1 //这是不工作url我有问题为什么这不工作
http://yourtradelog.com/api/getsymbol //这是有效的url
Database tables
<script>
$(document).ready(function() {
$('#exchange').change(function(){
var exchangeID = $(this).val();
if(exchangeID){
$.ajax({
type:"GET",
url:"{{url('api/getsymbol-request/')}}?exchange_id="+exchangeID,
success:function(res){
if(res){
$("#market").empty();
$("#market").append('<option>Select</option>');
$.each(res,function(key,value){
$("#market").append('<option value="'+key+'">'+value+'</option>');
});
}else{
$("#market").empty();
}
}
});
}else{
$("#market").empty();
}
});
});
</script>
{!! Form::open(['method'=>'POST', 'action'=> 'MemberTradesController@store']) !!}
<div class="form-group col-sm-5">
{!! Form::label('market_id', 'Markets:') !!}
{!! Form::select('market_id', [''=>'Choose Options'] , null, ['class'=>'form-control', 'id'=>'market'])!!}
</div>
<div class="form-group col-sm-10">
{!! Form::label('symbol_id', 'Symbol:') !!}
{!! Form::select('symbol_id', [''=>'Choose Options'] , null, ['class'=>'symbol_id', 'id'=>'symbol','data-width'=>'60%', 'data-live-search'=>'true'] )!!}
</div>
<div class="form-group col-lg-4">
{!! Form::submit('Add trade', ['class'=>'btn btn-success btn-lg']) !!}
</div>
{!! Form::close() !!}
我更喜欢像这样使用路由参数(更多信息请参阅文档 here):
路线:
Route::get('api/getsymbol-request/{id}','MemberTradesController@getSymbolRequest');
Route::get('api/getsymbol','MemberTradesController@getSymbol');
控制器:
public function getSymbolRequest(Request $request, $id){
$symbol = DB::table("symbols")
->where("market_id", $id)
->get()
->pluck("symbol","id");
return response() -> json($symbol);
}
public function getSymbol(){
$symbol = DB::table("symbols")
->pluck("symbol","id");
return response() -> json($symbol);
}
Please note that the URL has changed to this format:
试试这个代码:
public function getSymbolRequest(Request $request)
{
$marketID = (int)$request->get('market_id')
$symbol = DB::table('symbols')
->where('market_id', $marketID)
->get(['symbol','id']);
return response()->json($symbol);
}
public function getSymbol()
{
$symbol = DB::table('symbols')
->get(['symbol','id']);
return response()->json($symbol);
}