Laravel API 返回 JSON 格式的响应

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/h20101988/article/details/85846430

Passport 做的 API 认证,用 Postman 测试 API 时,在没有获取 token 的情况下访问接口,会自动跳转到登录界面,这显然不是我想要的,虽然可以设置 Headers 解决,但是我希望即使在浏览器端测试 API ,也是返回 json 格式的错误信息,而不是跳转到登录界面。

下面是解决办法:

1、新建中间件 JsonResponse

php artisan make:middleware JsonResponse

2、编辑 app/Http/Middleware/JsonResponse 文件:

public function handle($request, Closure $next)
    {
        if ($request->is('api/*')){
            $request->headers->set('Accept', 'application/json');
        }
        return $next($request);
    }

3、把 JsonResponse 添加到 Kernel 中,编辑 app/Http/Kernel:

protected $middleware = [
		......
		
        //Application Middleware
        \App\Http\Middleware\JsonResponse::class,
    ];

再次访问接口,会有如下响应:

{
  "message": "Unauthenticated."
}

https://www.hellocode.wang/article/laravel-api-json-response-VWbZXsR6

展开阅读全文

action响应ajax请求返回JSON类型

12-04

我在用ajax向action发送请求时,可以响应请求,但是ajax在获取数据时,获取不了json类型的数据,不知道是哪里出了问题。代码如下:rnrn jsp代码:rnrn rnrn rnrn rnrn rnrn rnrn 省份 rnrn rnrn rnrn --请选择所在省份--rnrn rnrn $proName rnrn rnrn rnrn rnrn rnrn rnrn 城市 rnrn rnrn rnrn --city-- rnrn rnrn rnrnrnrn rnrnrnrn rnrnaction代码如下:rnrnpackage action;rnrn rnrnimport java.io.PrintWriter;rnrnimport java.io.Writer;rnrnimport java.util.List;rnrn rnrnimport javax.servlet.http.HttpServletRequest;rnrnimport javax.servlet.http.HttpServletResponse;rnrn rnrnimport net.sf.json.JSONArray;rnrn rnrnimport org.apache.struts2.ServletActionContext;rnrn rnrnimport service.odservice.ICityService;rnrnimport service.odservice.IProvinceService;rnrn rnrnimport com.opensymphony.xwork2.ActionContext;rnrnimport com.opensymphony.xwork2.ActionSupport;rnrn rnrnimport entity.City;rnrnimport entity.Province;rnrn rnrnpublic class AddressAction extends ActionSupport rnrn /**rnrn * rnrn */rnrn private static final long serialVersionUID = 1L;rnrn private IProvinceService proService; rnrn private ICityService cityService;rnrn private String proCode;rnrn private String opts;rnrn rnrn public String getProCode() rnrn return proCode;rnrn rnrn public void setProCode(String proCode) rnrn this.proCode = proCode;rnrn rnrn rnrn public String getOpts() rnrn return opts;rnrn rnrn public void setOpts(String opts) rnrn this.opts = opts;rnrn rnrn public IProvinceService getProService() rnrn return proService;rnrn rnrn public void setProService(IProvinceService proService) rnrn this.proService = proService;rnrn rnrn public ICityService getCityService() rnrn return cityService;rnrn rnrn public void setCityService(ICityService cityService) rnrn this.cityService = cityService;rnrn rnrn rnrn public String selectAddress() throws Exception rnrn if(proCode==null)proCode="";rnrn System.out.println("proCode="+proCode);rnrn List citylist = cityService.findByProCode(proCode);rnrn List prolist = proService.searchAll();rnrn if(proCode!=null&&proCode!="")rnrn System.out.println(citylist.get(0).getCityName());rnrn HttpServletRequest request = ServletActionContext.getRequest();rnrn request.setAttribute("citylist", citylist);rnrn request.setAttribute("prolist", prolist);rnrn HttpServletResponse response = ServletActionContext.getResponse(); rnrn response.setContentType("application/json"); rnrn response.setCharacterEncoding("UTF-8"); rnrn PrintWriter writer = response.getWriter();rnrn JSONArray array = JSONArray.fromObject(citylist);rnrn opts = array.toString();rnrn writer.print(opts);rnrn return SUCCESS; rnrn rnrn 论坛

没有更多推荐了,返回首页