我正在訪問一個API,我必須首先發布登錄信息。下面是我通過什麼樣的一個例子,我也得到成功的連接:Ajax調用API並傳遞Set-Cookie
$.ajax({
type: 'POST',
url: 'https://server:port/api/users/_login',
contentType: 'application/x-www-form-urlencoded',
dataType: 'xml',
data: {username: "user", password: "userPassword"},
success: function(data, textStatus, jqXHR){ alert(textStatus); },
error: function(jqXHR, textStatus, errorThrown){ alert(textStatus); }
});
這是200響應:
Access-Control-Allow-Headers:Content-Type
Access-Control-Allow-Methods:GET, POST, DELETE, PUT
Access-Control-Allow-Origin:*
Content-Length:125
Content-Type:application/xml
Date:Fri, 21 Oct 2016 08:49:21 GMT
Expires:Thu, 01 Jan 1970 00:00:00 GMT
Server:Jetty(9.2.14.v20151106)
Set-Cookie:JSESSIONID=h9823inizvhd2793nuqw7;Path=/api;Secure
任何後續調用必須與JSESSIONID一起做。所以我寫了另一個電話如下 - 註釋字段將告訴你我已經試過,並打出一輪:
$.ajax({
type: 'GET',
url: 'https://server:port/api/datareq/guid/stats',
dataType: 'xml',
//crossDomain: true,
xhrFields: { withCredentials: true },
//data: data,
success: function(data, textStatus, jqXHR){ alert(textStatus); },
error: function(jqXHR, textStatus, errorThrown){ alert(textStatus);}
});
如果我不包括「withCredentials」(或將其設置爲false),我只是得到401:在通話中未經授權。但是,如果我確實使用了該標誌,則會收到以下消息:
當憑證標誌爲true時,通配符'*'不能用於'Access-Control-Allow-Origin'標頭中。因此不允許原產地'null'訪問。 XMLHttpRequest的憑證模式由withCredentials屬性控制。
現在我明白髮生了什麼,但我無法想象它的方式。閱讀相當多的帖子,但沒有真正的工作。不幸的是,我無法訪問api Web服務器以對該側進行任何更改,這是一個鎖定設備。我將繼續關注一些相關文章,但如果有人知道並且可以在解決方法上略微提出一些看法,那麼將非常感激。
測試平臺:的Windows 8.1
瀏覽器:的Chrome 53.0.2785.143(正式版本)M(32位)
謝謝,那就來看看返工的方法吧。令人失望:/ – Amble