假设我有这样的Vue组件
export default {
created() {
axios.get("a url").then(res => {
console.log(res.data);
});
}
};
然后axios在laravel控制器中向该功能发送请求
public function something()
{
$data = Model::all();
$comments = Comment::all();
// here i want to send both $data and $comments to that view
return response()->json();
}
我可以同时发送它们吗? 这个组件和功能只是一个例子
答案 0 :(得分:1)
在这里,您可以创建一个关联数组并通过json发送。
import requests
def check_url(url, timeout=3):
headers = {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36'}
try:
response = requests.get(url, headers=headers, timeout=timeout)
except requests.exceptions.Timeout:
print(f'{url} time-out')
return False
except requests.exceptions.ConnectionError:
print(f'{url} Connection Error')
return False
else:
return response.status_code == 200 # what if redirect? is it possible
urls = [...]
for url in urls:
print(f"{url} : {'Exists' if check_url(url=url) else 'Not Exists'}")
在Vue中,您可以编写如下内容。
public function something()
{
$data=Model::all();
$comments=Comment::all()
return response()->json([
'data'=>$data,
'comments'=>$comments
]) //here i want to send both $data and $comments to that view
}
答案 1 :(得分:0)
您可以简单地做到这一点
public function function()
{
$data=Model::all();
$comments=Comment::all()
return response()->json([
'data'=>$data,
'comments'=>$comments
]) //here i want to send both $data and $comments to that view
}
答案 2 :(得分:0)
您只需使用laravel的response
方法即可实现
public function something()
{
$data=Model::all();
$comments=Comment::all()
return response()->json([
'data'=> $data,
'comments'=> $comments
], 200);
}
或使用相同方法的另一种方式
public function something()
{
$data=Model::all();
$comments=Comment::all()
return response([
'data'=> $data,
'comments'=> $comments
], 200);
}
在您的组件中,您只需使用即可获取数据
export default
{
created()
{
axios.get("a url")
.then(res=>{
console.log(res.data.data)
console.log(res.data.comments)
}
}
}
谢谢