2011-06-15 136 views
10

我正在尝试对iContact API执行一个请求,该请求需要我使用自定义标头进行验证(http://developer.icontact.com/documentation/authenticate-requests)。这是我的代码:jQuery AJAX自定义标头

$.ajax({ 
     type: "GET", 
     url: "https://app.icontact.com/icp/a/", 
     contentType: "application/json", 
     beforeSend: function(jqXHR, settings){ 
       jqXHR.setRequestHeader("Accept", "application/json"); 
       jqXHR.setRequestHeader("Api-Version", iContact_API_version); 
       jqXHR.setRequestHeader("Api-AppId", iContact_appID); 
       jqXHR.setRequestHeader("Api-Username", iContact_username); 
       jqXHR.setRequestHeader("API-Password", iContact_appPassword);} 
}); 

由于某种原因,请求没有通过。但是,当我手动执行相同的请求(使用Chrome REST控制台)时,它工作得很好。如果我取出自定义标头(API- *),请求会通过,但当然身份验证会失败,并返回一个常规的HTML页面。

我切换到Firefox和检查请求/响应头:

请求:

Host app.icontact.com 
User-Agent Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.6; en-US; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.12 
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 
Keep-Alive 115 
Connection keep-alive 
Origin http://184.72.61.244 
Access-Control-Request-Me... GET 
Access-Control-Request-He... api-appid,api-password,api-username,api-version 

响应:

HTTP/1.1 302 Found 
Date: Tue, 14 Jun 2011 23:43:56 GMT 
Server: Apache/2.2.9 (Debian) 
Set-Cookie: intellicontact_phpsess=1c7ca333017b47f46edd893dae584781; path=/; domain=.icontact.com; secure; HttpOnly 
Expires: Thu, 19 Nov 1981 08:52:00 GMT 
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0 
Pragma: no-cache 
Location: https://app.icontact.com/icp/login/sentry.php?relurl=https%3A%2F%2Fapp.icontact.com%2Ficp%2Fa%2F&sess= 
Vary: Accept-Encoding 
Content-Encoding: gzip 
Content-Length: 20 
Connection: close 
Content-Type: text/html; charset=utf-8 

任何想法是怎么回事错在这里?

谢谢!

+0

你可能看一个OPTIONS请求,这是寻找目标主机是否允许跨域请求,[详细](http://stackoverflow.com/questions/1256593/ jQuery的,为什么-AM-I-得到-的选项,请求insted的-的-A-获得请求)。解决方法是使用JSONP,但这不支持自定义标头,[这是为什么](http://stackoverflow.com/questions/3350778/modify-http-headers-for-a-jsonp-request)。如果iContact不支持跨域ajax请求或JSONP,最终可能会使用[cURL](http://en.wikipedia.org/wiki/CURL)等。 – 2012-08-28 14:12:58

+0

[JSON Post with Customized HTTPHeader Field]的可能重复(http://stackoverflow.com/questions/7100294/json-post-with-customized-httpheader-field) – JAAulde 2014-02-20 01:12:28

+0

http://stackoverflow.com/a/14655768/ 1581725 – 2015-04-18 01:19:12

回答

0

奇怪的是,您在响应中获得了302(Redirection)。您是否尝试过使用Ajax功能记录错误,以解决问题?这是我使用的一个例子。

console.log(" call back url :"+ callBackURL); 
       $.ajax({ 
        url: callBackURL, 
        type: 'post', //'GET' in your case  
        contentType: 'application/json; charset=utf-8', 
        dataType: 'json' , // explicitly mentioning datatype 
        success: function(json) { 
         console.log(" reponse :"+ json); 

        }, 
        error: function (XMLHttpRequest, textStatus, errorThrown) { 
          console.log("error :"+XMLHttpRequest.responseText); 
        } 


       }); 
-1

请试试这个肯定你可以得到问题解决。

function FormSubmit() 
    { 
    $.ajax({  
    type: "POST", 
    url: 'index.php', 
    data: $("#form_main").serialize(), 
    async: false }).done(function(data) { 
    $("#Response").hide(); 
    if (isNaN(data)) { 

    dispmsg = "<div class='alert alert-danger' role='alert'>Oops..Something had gone wrong! Please try again!</div>"; 
    $("#assess_me_response").html(dispmsg); setTimeout('ResetForm();',3000); 
    } 
    else { 

    dispmsg = "<div class='alert alert-success' role='alert'>Thank you for sharing the details. Our Consultant will contact you shortly!</div>"; $("#assess_me_response").html(dispmsg); setTimeout('Redirect();',3000); 

    } 
     return true; 
     }); 
    } 
+2

通常情况下,如果答案包含解释代码意图做什么的解释,以及为什么解决问题而不介绍其他问题,答案会更有帮助。添加“尝试这个”不算。 – 2015-02-16 05:57:34

+0

您的代码是如何设置自定义标题的? – Drakes 2015-04-16 10:58:11

1

的beforeSend钩,如果你想将其添加只仅适用于$.ajaxSetup()所以$.ajax()你根本就与headers财产去。


如果你想自定义页眉(或套头)添加到一个单独的请求,则只需添加标题属性:

// Request with custom header 
$.ajax({ 
    url: 'foo/bar', 
    headers: { 'x-my-custom-header': 'some value' } 
}); 

如果你想添加一个缺省的头部(或头的设定)每个请求然后使用$.ajaxSetup()

$.ajaxSetup({ 
    headers: { 'x-my-custom-header': 'some value' } 
}); 

// Sends your custom header 
$.ajax({ url: 'foo/bar' }); 

// Overwrites the default header with a new header 
$.ajax({ url: 'foo/bar', headers: { 'x-some-other-header': 'some value' } }); 

如果你想添加页眉(或Se头)的每一个请求的T,然后用beforeSend钩与$.ajaxSetup()

$.ajaxSetup({ 
    beforeSend: function(xhr) { 
     xhr.setRequestHeader('x-my-custom-header', 'some value'); 
    } 
}); 

// Sends your custom header 
$.ajax({ url: 'foo/bar' }); 

// Sends both custom headers 
$.ajax({ url: 'foo/bar', headers: { 'x-some-other-header': 'some value' } }); 

编辑(详细信息):有一点要注意的是,随着ajaxSetup只能定义一组默认头你只能定义一个beforeSend。如果多次拨打ajaxSetup,则只会发送最后一组标题,并且只执行最后一次发送前回拨。

来源:https://stackoverflow.com/a/14655768/1581725