weixin_33675507 2015-04-10 21:06 采纳率: 0%
浏览 21

jQuery Post提供休息服务

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);
        }
    });
  • 写回答

1条回答 默认 最新

  • 笑故挽风 2015-04-10 21:29
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!