【发布时间】:2019-03-11 12:25:34
【问题描述】:
我正在尝试向服务器发出 ajax 发布请求,以便我可以获取数据来更新我的视图
我的 Ajax 代码
$(document).ready(function() {
$.ajax({
async: true,
method:'POST',
url:'{{route('pusher')}}',
headers: {
"Content-Type": "application/json",
'X-CSRF-Token': '{{ csrf_token() }}'
},
data:{
mac:'{{$slaves['mac']}}'
},
success:function(response)
{
alert(response);
},
error:function(iqXHR,testStatus,errorThrown)
{
alert('error');
}
})
});
API
public function getSlaveDataJson(Request $request)
{
$temp = [
'action'=>'list_slave',
'mac'=> $request->mac,
'type' => 'all',
'appkey' => '3E6157D4409B28627586E37F7B1E656E'
];
json_encode($temp);
$response = Curl::to('http://103.31.82.46/open/open.php')
->withContentType('application/json')
->withData($temp)
->asJson(true)
->withHeader('Postman-Token: d5988618-676e-430c-808e-7e2f6cec88fc')
->withHeader('cache-control: no-cache')
->post();
self::destroy($request->mac);
self::addAllSlaves($response);
return $response;
}
如果我使用 postman 运行 API,API 会返回 json 数据,但是当我从 ajax 运行它时会出现 500 内部服务器错误。帮助。
附言 使用 websockets 代替它会帮助我创建一个自动更新的表吗?
【问题讨论】:
-
请显示您的路线定义
-
尝试返回 json 作为响应,例如 return response()->json([ 'data' => $response ]);
-
我通过使用 Fetch API 让它工作了!