Tôi cần thực hiện yêu cầu getJSON()
, nhưng làm cách nào để chuyển ủy quyền và tiêu đề tùy chỉnh?Làm cách nào để chuyển các tiêu đề yêu cầu bằng phương thức getJSON() của jQuery?
Tôi gặp sự cố khi tiêu đề yêu cầu lấy tên, nhưng KHÔNG phải là giá trị. URL đang được hiển thị thông qua yêu cầu thủ công trong phần mềm không được chèn vào dưới dạng tùy chọn thay vì GET/Url.
Đây là một ví dụ về những gì chúng tôi đang cố gắng để làm điều đó hoạt động tốt trong fiddler; làm thế nào tôi có thể tái tạo điều này với chức năng AJAX? đang
GET /Service.svc/logins/gdd53535342/houses/vxcbdfsdg/people/dsgsdggd?format=json HTTP/1.1
User-Agent: Fiddler
Authorization: Basic rgbg423535fa23y4436
X-PartnerKey: df3fgeg-g5g6-b55b-f3d2-dsgg353523
Host: 154.34.53.54:2757
JavaScript:
xhr = new XMLHttpRequest();
$(document).ready(function() {
$.ajax({
url: 'http://localhost:437/service.svc/logins/jeffrey/house/fas6347/devices?format=json',
type: 'GET',
datatype: 'json',
success: function() { alert("Success"); },
error: function() { alert('Failed!'); },
beforeSend: setHeader
});
});
function setHeader(xhr) {
xhr.setRequestHeader('Authorization', 'Basic faskd52352rwfsdfs');
xhr.setRequestHeader('X-PartnerKey', '3252352-sdgds-sdgd-dsgs-sgs332fs3f');
}
Fiddler Bình thường Request Headers:
GET /service.svc/logins/jeffrey/house/fas6347/devices?format=json HTTP/1.1
User-Agent: Fiddler
Authorization: Basic faskd52352rwfsdfs
X-PartnerKey: 3252352-sdgds-sdgd-dsgs-sgs332fs3f
Host: localhost:437
Fiddler Qua Ajax()
Headers Yêu cầu:
OPTIONS service.svc/logins/jeffrey/house/fas6347/devices?format=json HTTP/1.1
Host: localhost:437
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.6) Gecko/20100625 Firefox/3.6.6
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
Proxy-Connection: keep-alive
Origin: http://ipv4.fiddler:61975
Access-Control-Request-Method: GET
Access-Control-Request-Headers: authorization,x-partnerkey
Nếu bạn đang làm điều này cho một dịch vụ JSONP, bạn không thể đặt tiêu đề phụ. – Pointy
không chỉ là một json bình thường. – Berty