我正在尝试使听者中的corsOrgin能够使用Ajax从laravel应用程序中使用剩余api。但是,在客户端控制器中编写@CrossOrigin(origins =“ http://localhost:8080”)之后,它向我显示警告CrossOrigin无法解析为类型。有人知道如何解决此问题吗?
@CrossOrigin(origins = "http://localhost:8080") problem
Client.Controller
$("#a1").on("click", "a", function() {
var lang = $(this).data("lang");
window.location = "http://flysmarter."+lang;
});
pom.xml
@CrossOrigin(origins = "http://localhost:8080")
@RequestMapping(value = "/clients", method = RequestMethod.GET)
@Produces({ MediaType.APPLICATION_JSON })
public ResponseEntity<?> getAllClients() {
List<Client> clients = clientService.loadAll();
return new ResponseEntity<>(clients, HttpStatus.OK);
}
答案 0 :(得分:0)
只需在服务器端设置响应标头,例如res.setHeader("Access-Control-Allow-Origin", "*");
。
//@CrossOrigin(origins = "http://localhost:8080")
@RequestMapping(value="getDeatils", method=RequestMethod.GET, produces="application/json" )
public ResponseEntity<JSONObject> getTinDetails(HttpServletRequest req, @RequestParam("gstin") String gstin, HttpServletResponse res) {
res.setHeader("Access-Control-Allow-Origin", "*");
return new ResponseEntity<JSONObject>(responceObj, HttpStatus.OK);
}else{
throw new UserException("_GSTC_INVALID_GSTIN_");
}
}
只需尝试一下,它就对我有用。如果有助促进