I'm querying a REST webservice which uses custom http headers for authentication.

我正在查询一个使用自定义http头进行身份验证的REST web服务。

If I perform a POST without the headers I'm getting the expected error, but when I add the headers I get a 404 error instead of what I actually need.

如果我执行一个没有标题的帖子,我将得到预期的错误,但是当我添加标题时,我将得到一个404错误,而不是我实际需要的错误。

This is my code

这是我的代码

$.ajax({
  type: 'POST',
  url: 'http://server.com/service',
  beforeSend: function (xhr) { xhr.setRequestHeader('CUSTOM-HEADER-KEY', 'CUSTOM-HEADER-VALUE') },
  success: function(data) {    
    alert('success.');
  }
});

Here's the firebug headers output:

下面是firebug头输出:

OPTIONS /service HTTP/1.1 Host: server.com User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:8.0.1) Gecko/20100101 Firefox/8.0.1 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,/;q=0.8 Accept-Language: en-us,en;q=0.5 Accept-Encoding: gzip, deflate Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7 Connection: keep-alive Origin: null Access-Control-Request-Method: POST Access-Control-Request-Headers: custom-header-key Pragma: no-cache Cache-Control: no-cache

选项/服务HTTP/1.1主机:server.com用户代理:Mozilla/5.0 (Windows NT 6.1;WOW64;Gecko/20100101 Firefox/8.0.1 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,/;q=0.8 Accept- language: en-us,en

and the smae headers when performing the post with poster, which returns desired result.

以及smae在使用海报执行贴子时的标题,它返回期望的结果。

POST /service HTTP/1.1 Host: server.com User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:8.0.1) Gecko/20100101 Firefox/8.0.1 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,/;q=0.8 Accept-Language: en-us,en;q=0.5 Accept-Encoding: gzip, deflate Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7 Connection: keep-alive CUSTOM-HEADER-KEY: CUSTOM-HEADER-VALUE Pragma: no-cache Cache-Control: no-cache Content-Length: 0

POST /service HTTP/1.1 Host: server.com用户代理:Mozilla/5.0 (Windows NT 6.1;WOW64;Gecko/20100101 Firefox/8.0.1 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,/;q=0.8 Accept- language: en-us,en

The difference is pretty obvious, but I don't know what I'm doing wrong in the jquery code.

差别是很明显的,但是我不知道jquery代码中我做错了什么。

Could anyone help me please?

谁能帮帮我吗?

4 个解决方案

#1


5

In a cross domain request if the header is not allowed by the service browser will simply remove it

在跨域请求中,如果服务浏览器不允许页眉,则只需删除它

At first browser browser will make an OPTION call to check for the allowed (Origin, Headers, Methods)

首先浏览器会发出一个选项调用来检查允许的(来源、标题、方法)

In your service configuration you have to allow the header in order to be able to send it to server using

在您的服务配置中,您必须允许报头,以便能够使用

Access-Control-Allow-Headers: YOUR_HEADER_NAME

更多相关文章

  1. Chrome显示错误为:由于内容安全策略,拒绝执行内联脚本
  2. Java applet错误…怎么了?
  3. 9.1.4 前端 - HTML body标签 - 标题,段落,分割线,换行,特殊符号,列
  4. Jquery - 表单验证,为错误消息添加css样式
  5. jQuery验证没有错误计数
  6. 纯CSS最小化高度标题,绝对定位DIV内最大化身高
  7. GET错误Glyphicon-halflings-regular.ttf / woff / woff2与来自L
  8. 如何在离子框架中显示图像标题?
  9. iis aspx常见错误 HTTP 错误 404 - 文件或目录未找到。

随机推荐

  1. Android Manifest标签之manifest,meta-da
  2. 【Unity3D】Unity3D与Android的交互通信(A
  3. Android(安卓)Studio 简单介绍和使用问题
  4. android开发资源合集
  5. Android: TextView常用属性的用法详解
  6. [置顶] 对Android(安卓)MVVM的理解
  7. Android中MPAndroidChart自定义绘制最高
  8. 【Android开发】基本组件-图像视图
  9. 在eclipse的android工程里引用android sd
  10. android studio 完整安装教程,已完全实践