我尝试从xhtml文件执行ajax查询,并且其拖曳错误: 未捕获到的SyntaxError:无效或意外的令牌
这是我的ajax查询:
<h:form>
<script type="text/javascript">
llamadaNombre();
function llamadaNombre(){
var list2=null;
$.ajax({
type : "GET",
url : "localhost:8080/users",
dataType: "json",
data: {val: list2},
success : function(result) {
$('#tablebody').html("");
$.each(result,function(key, val) {
editar="<h:commandLink action='/paginas/crud/editarCliente?dni="+val.dni+"' class='btn btn-success'> <span class='glyphicon glyphicon-search'></span> editar </h:commandLink>" ;
eliminar="<h:commandLink action='/paginas/crud//eliminarCliente?id="+val.dni+"' class='btn btn-success'> <span class='glyphicon glyphicon-trash'></span> eliminar </h:commandLink>";
var htmlrow ="<tr>"+"<td>" + val.id + "</td>"+"<td>" + val.nombre + "</td>"+"<td>" + val.apellidos + "</td>"+"<td>" + val.dni + "</td>"+"<td>" + val.correo + "</td>"+"<td>" + val.telefono + "</td>"+"<td>" + val.direccion + "</td>"+"<td>" + val.cp + "</td>"+"<td>" + val.edad + "</td>"+"<td>" + editar + "</td>"+"<td>" + eliminar + "</td>"+"</tr>";
$('#tablebody').append(htmlrow);
})
}
})
}
</script>
</h:form>
api rest中的数据格式为json
在springBoot和JSF应用程序中有什么方法可以连接api?它抛出错误:
jquery.min.js:2 Access to XMLHttpRequest at `'htttp://localhost:8080/users?val=' from origin 'http://localhost:9090' has been blocked by CORS policy: Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https.`
i尝试在不同的环境中更改端口,但它会不断返回该错误,如果在同一端口中进行配置,则将引发该端口侦听的错误
这是我的API控制程序:(已解决)
@Service
@CrossOrigin(origins = "http://localhost:8080")
public class UsuariosController {
public List<Clientes> getAll(){
return clientesFacade.getAllClientes();
}
答案 0 :(得分:0)
您应该将标题参数作为波纹管脚本添加到您的请求中:
$.ajax({
type : "GET",
url : "localhost:8090/users",
data: {val: list2},
headers: {
"X-Requested-With": "XMLHttpRequest",
"Authorization": .......,
},
success: function (data, text) {
console.log(data);
},
........
在您的Java控制器中,您应该授权从本地主机访问,您可以在服务中添加注释:@crossorigin(origins = "localhost" )
。