I´m trying to access a REST service using the post method but I get this response.
XMLHttpRequest cannot load http://192.168.0.13:8090/PruebaRestEjemplo/api/entidades.usuarios. Request header field Content-Type is not allowed by Access-Control-Allow-Headers.
But the content type header is allowed, I can try my services with postman and I get no error. What can it be? this is my code.
var prueba = new Object();
prueba.nombreUsuario = "Miguel";
prueba.apellidoUsuario = "De Cervantes";
prueba.usuarioLogin = "MCervantes";
prueba.contrasenaLogin = "Mcervantes";
$.ajax
({
type: "POST",
url: url,
contentType:"application/json; charset=UTF8",
dataType:"json",
data: prueba,
async: false,
beforeSend: function (xhr){
xhr.setRequestHeader('Authorization', x.basicAuth(x.usuario, x.clave));
},
success: function(result){
callback(result);
}
});
Rest Web services always take the data in json or XML format and while posting the data to a web service you need to serialize the javascript object into JSON or XML object as specified in web service. In your code the prueba is the javaScript object and you need to serialize the object to make it JSON or XML which ever is supported by your web service. You can use this function just before the Ajax call.
JSON.stringify(prueba);
Hope it helps.