Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set headers in ajax call with jquery

I need to call an Office 365 Rest API from my own application.

When I copy and paste the url on the same browser session, I can see some XML.

enter image description here

If I paste that URL into an incognito window I get this error:

The custom error module does not recognize this error.

I am trying to make an API call to get that response from my application, when checking with developer tools, how they call the service I can see this:

enter image description here

As you can see the API call has some request headers.

Google chrome has the functionality to copy that as a CURL request.

And this works for me:

curl "https://portal.office.com/api/myapps/GetAllApps" -H "AjaxSessionKey: wEAjKvw1WCmLD27I2TGqAuga25rq5HdKrdEOCOTXhfX4k6H3U/AQru+hPWfWSMX0hhQ++OFkm/FvKN+Z1moC1Q=="
-H "Accept-Encoding: gzip, deflate, sdch, br" -H "X-SuiteServiceProxyOrigin: https://outlook.office365.com" -H "Accept-Language: en-US,en;q=0.8" -H "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.116 Safari/537.36" -H "Content-Type: application/json;charset=UTF-8" -H "Accept: application/json" -H "Referer: https://portal.office.com/SuiteServiceProxy.aspx?upn=luis.valencia"%"40softwareestrategico.com&suiteServiceReturnUrl=https"%"3A"%"2F"%"2Foutlook.office365.com"%"2Fowa"%"2F&returnUrl=https"%"3A"%"2F"%"2Foutlook.office365.com"%"2Fowa"%"2F"
-H "Cookie: s.SessID=6bcb4983-767b-41b4-8bc9-03d5df23fab7; p.CachedJSFiles=16.00.1449.010:0x27F042160xD2810E3C0xF5EAFC860xA82B20870x58AB93C50x469628490xA2E1E0750xD5297DF50x63CBC2C30xF07895570x76AC56DF0xF515B60E0x052D52250xE77D86F40xD6CD36BB0xF5394BE50x0CA8EA080x810AC8B70xFFDEDE890x59EBF4680x117A18140xFCB544560x2E5289740x883529F50xA732E006; p.CachedCSSFiles=16.00.1449.010:0xAFCABDD50x7704885F0x1EC8288A0x2A2173270x7A142B580x630DEBB30x146543E00x49F2D68A0x9EA9D3370x8ED766C20xFD9BA3040xF4175814; p.U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861U0SQHfw4RTnQhvt8qjTrYDkmrvtEh8Ba/kPn7O7KpUMnODVRpCnCcIIC0BTNSdbAntlNHaA==; s.AjaxSessionKey=wEAjKvw1WCmLD27I2TGqAuga25rq5HdKrdEOCOTXhfX4k6H3U/AQru+hPWfWSMX0hhQ++OFkm/FvKN+Z1moC1Q==; s.userid=10037FFE9574B4FF; s.LoginUserTenantId=YSFQ8kyfSxMbyHdGGe3qeVi0Uug+6O1jT+ZVn5rXNvX9QvrR6VUpwYwG9HCZUaeyPZwzHuW/1zGwV179588ts2OVGQvmAo5Fvs07lM8ARoKOgyg7UxVdhvVsWU8cDWHiP4Is7P9PdSBPPoDQFjRTpg==; p.FirstLoginDateTimeUtc=id=-1406574579&value=Jan_27_2016; MUID=0F3BE9304DCA6D792017E0994C5A6C86; p.TenantCulture=f5b4bce5-06f0-4035-861f-ddea6d55a5e9::es-CO; s.TagCacheKey=fMdZZLwxq70P5SVbV794Vbf/Nb6nrvfWN+sIz3KSTqCAUfDP6dbCYDg6kiwPPZmjo0A8m48rtqF1Ir450dSxl9Y/qWu+a5gYRmTOZh0kXi8bndA40br4f+YO2xmVdZ3SpkSjwknoddE1meso8NTWvi/lL5+oKy5UOMB4B/YPRkunc7y6z0LWzq+xHgorBWo+ccL4JRzeTdThlxVDd23IcPO/w4O9+O51AHtw3qkZ7kvBiCZcooMX46yGP0SMnsZ0qjg8cv0o01w15/X7rdLQTTut7idyAsvhGsdlOLY8ghE=; s.BecContext=AAAAAQAAAAAEEGiA2yjQd8hDt7bHKBd5pSsGCWCGSAFlAwQCAQYJYIZIAWUDBAIBBglghkgBZQMEAQIEILiXEtiztm51j+9eensRmodQaco4fuQKKMwqb9FVtJxdBBDLmOAQDw0294WzUuRL6prcIIIBsJMrwukBvRsIudZmuuqrcO0I+DdJo5/fMTl+8zD3knXxHpckcUeBtdiqx/OPKiMF2i2gyqd7KV5p7E3yFPIsNs9UEf/A27qVOtZdvOh5YXX76MK0zjSN/19mSRLFu0sAdbIJ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1b7jxef9qf4dU838fvcfjM4iMngm5qqyb6LTTETcM24m9s2fq9UeECtDNVM7bgfXR6LOfWYHX52cCZyLUwazZH/Jqu84hoUE1HrEe/XDBMsrgFFxelLH+smw53fx7UBBaxsGw2ca83bl2VbUvDpV5eo9a45/9yz3lEsXsLyPT3jXMtLuuOcu54g+gX8SJ/Pc4p/VO9bXCjodP8JmyHgMnXJ+7QxW8cht0LNIJL0HnqubS/L2Kq8+u2kwc/Co5S431yuFFp6OUoR52pvNJmM9i7ZHogvAKlbA1tYdCSuormfJZYIQII4zr4vAkfw/xBG8qxzuMnf0U0Og8UEIch2PIXyjAg5ew6pQUlC2RC92IdDl5rQFMGPA9XefcehUZ43OTw+x3iSFsYyJwhJYWylN0mZq6AKWroXESuGf7KOT8RPztwcxgrnW+x0jRa2jzYNVPYrYZTzgCY+0QTjk8mBrNyKS10X5aCOo41cNY9hile/WHQJCHxoEArBHDJUCQue9cSj3+d/SfsNFnT13KUGBzMlEO91sGsRtU01uQNJSVD5i4JwBs5F4eK/Qz3+F7yzBNes03s8Zs4SnqSvOgBrIx/4F67w+nEARxuthkSKVl0IWbqNO8vBEWSm3A=="
-H "Connection: keep-alive" --compressed

I can see some response as below: enter image description here

Now I am trying to do the same with $ajax

$.ajax({
    url: 'https://portal.office.com/api/myapps/GetAllApps',
    type: 'GET',
    dataType: 'json',
    data: {
        format: 'json'
    },
    beforeSend: function (xhr) {
        xhr.setRequestHeader('Accept', 'application/json');
        xhr.setRequestHeader('Accept-Encoding', 'gzip, deflate, sdch, br');
        xhr.setRequestHeader('Accept-Language', 'en-US,en;q=0.8');
        xhr.setRequestHeader('AjaxSessionKey', 'wEAjKvw1WCmLD27I2TGqAuga25rq5HdKrdEOCOTXhfX4k6H3U/AQru+hPWfWSMX0hhQ++OFkm/FvKN+Z1moC1Q==');
        xhr.setRequestHeader('Connection', 'keep-alive');
        xhr.setRequestHeader('Content-Type', 'application/json;charset=UTF-8');
        xhr.setRequestHeader('Cookie', 's.SessID=6bcb4983-767b-41b4-8bc9-03d5df23fab7; p.CachedJSFiles=16.00.1449.010:0x27F042160xD2810E3C0xF5EAFC860xA82B20870x58AB93C50x469628490xA2E1E0750xD5297DF50x63CBC2C30xF07895570x76AC56DF0xF515B60E0x052D52250xE77D86F40xD6CD36BB0xF5394BE50x0CA8EA080x810AC8B70xFFDEDE890x59EBF4680x117A18140xFCB544560x2E5289740x883529F50xA732E006; p.CachedCSSFiles=16.00.1449.010:0xAFCABDD50x7704885F0x1EC8288A0x2A2173270x7A142B580x630DEBB30x146543E00x49F2D68A0x9EA9D3370x8ED766C20xFD9BA3040xF4175814; p.UnAuthUserCookie=bb7622b1-75ba-49fe-ae48-feb7c77acba6; s.RPSClearCT=5j5ZGIOBjVgy5L8SUAexNuSViRkEHY5vXGshfJRnDOLJBGgtv9gbmBX03FDDg5hCygEMNgPp7JopPYGo9VtjKAjTXevaezBIVF0EE4HWxTKBpz7h+5G7ZHPNtwV3C3C27zp1XVbtPpA0tHyDzKnweUJtkPoBmQ2UeXpZYcGDAzVFExD8bZwk0RfNm4txuELilgX0Z+s7jRkh5dRu7MFaHH+naob4+2RJelaxcD7fBRGMT1kYloKJfgXihMfdRB09ZjT/egCmiT57jhGSTkIZfN4wIlAm2BzlQ4XBQ0kbLfG+bm/4m9OMITtQOf8Czj/8mlYzksaeGmVzf03syDlJgb4fYoDcMVQ4gUQBtLRZeaDhiIUQDBnTW70XHs4hpaHJCweSyfxrzvXzQSIg9NBe5a30he42cTaMDgY9XmzJxN5r1I32zAoYjN/gjdW4/q+zK+yeN3oNgHD8IufgUFEcdMCBa+CsgVPox9fqYlXJ0c25ksXgRTS6txYe0iHCwZlQgXLKslbjC7k1suHvE7jbCJ5pFfiwDxsISNegC26ENmNoVSXLVP3lwkvwnCMXywtMgiyP+hYjc30EI4inIC2pto9OhECztFscN46mCSBx/d/uTGMVJoSp3I6yikO522/UilLD7/eTeZj2/K0bwOJLVXeoHBNlcLb174JuKZI3KhmcMxFyuo3lWw3lE1YfWLDFqT5VE2eyX5ozk94xKtoWVOvf4MMEPPIMlQmu/gENl3GpegabsLdRr7MHQB16juRl2VS8yitoDrgp3H/d/9DyP8YTIDP3sF5Zdk2FWb/C+Avj6c9M3NR8WhO56rojCO0FkRnOo6uhDxNcVzRNulOs4+G7Sfgt9KOR7iYw3dEe6RrXccR3rp7wuHsdQfmqbbBq6/shW96m9tJuPPDIsYx7XmPU5TtvzFwOUkkLRcEbUd1MqwZknJVlHvbBc69nsc7dbBOQbJF07PfXyF064jjQDWQx0qWUoCRBHxoQNAv6i0lU1z0BTIm3JSU5ZsAZMzyWxzxZFVaJhXdZ861U0SQHfw4RTnQhvt8qjTrYDkmrvtEh8Ba/kPn7O7KpUMnODVRpCnCcIIC0BTNSdbAntlNHaA==; s.AjaxSessionKey=wEAjKvw1WCmLD27I2TGqAuga25rq5HdKrdEOCOTXhfX4k6H3U/AQru+hPWfWSMX0hhQ++OFkm/FvKN+Z1moC1Q==; s.userid=10037FFE9574B4FF; s.LoginUserTenantId=YSFQ8kyfSxMbyHdGGe3qeVi0Uug+6O1jT+ZVn5rXNvX9QvrR6VUpwYwG9HCZUaeyPZwzHuW/1zGwV179588ts2OVGQvmAo5Fvs07lM8ARoKOgyg7UxVdhvVsWU8cDWHiP4Is7P9PdSBPPoDQFjRTpg==; p.FirstLoginDateTimeUtc=id=-1406574579&value=Jan_27_2016; MUID=0F3BE9304DCA6D792017E0994C5A6C86; s.TagCacheKey=Tyo3H5iedT2v3VBQUIPR8X17RI2TPN7NbZ57l5cTNIu2XneEraxvGuohA6b+30tBn15x7XkXLKyjJjJSI7EX5f19nlvrpYcdHo8vRgCFjNVRaXFOqX5tjfVQ+3egiYh6xiKFxRK5RikgIhi+qHdUCgAAh3ubFGhsHdrqp4C9Z2vuKxNGGEIbytPAVslISKTRjlt8LrsaG0P8oqKIrLRgR0vFjbL3XZD5OV+38GpP+deHvLAJBc62HMpOrjqGcQF8FXZK9jRcAJrIgelXogXnINvqvmUTDHyn/l9J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cNqSgNhOXol5F9wZ8Ri/9M2Vg/Z+YRp2CRi6Xyr5mxmUrNg2UIyejNOFQYQRw7VJnFdjTeluHwNYz5/zKIi5HPITW71b/B/1yBSvJABUOFdQgdepSSl6QwnsB9+1upwQWj4pE+6Fx1eiuTKO1cGT/GclAAMz8EyWjJj1vJNJJLxutjVZmbeACH9DiLEc7Vr/WsHTJv0VLJ2rjq7U3dDxqQ7GDzaiSEuwIP17WE2uWNUAg==; s.BecContext=AAAAAQAAAAAEEGiA2yjQd8hDt7bHKBd5pSsGCWCGSAFlAwQCAQYJYIZIAWUDBAIBBglghkgBZQMEAQIEIIimBSyEKl7CJv/2+fJ7kQWgf8ZFrE2rS5N4Q883AYGcBBDHkGvCyq58OtBctX3PiAbrIIIBsHHMMgDeIEwWcwFehoQJuPt6f1UvdJmidNryc/XXmTeGBKJn733KDzNXKjNmoREt/e59CGXSjpA97aD7b5Sjbjo9ubQ3gnFfykEZ5hePlMrbYgzNtMnRDpQ7UKIevkGU8K+REqUJPZqE4tcyi7gdnNXy/RlWfeaSGerL/6rZIm8OxTmtnIGCm7Hiw5CqLZ1PgCJuRDegh2VPJtI3s37pQC8o+81f02LOxQ2x/zV0BFH5dG9mLOGBomCEE5dgvMpjaE46mjK8jO9Z8JWzILVFS6H/ZxnW2ua9XJauMcgJrNjSTg6M+4ZEKbabDi0ACSdDTdekDJxN2edO0V4sLxNlTm7yyQJ1JLbcNlJWBJdrRS7GpXgl9n0byI/6GBDzTPHluqpQjirJ+2sFT7Y9ksetXDCE4VZTG6szx8igi1rc5vzyxWdfsc3rovUKMMNJM0aIXArMXYDN85C8K61X9gq+1iArr5fWGch+VwsVYWdXZANgYZDrnqknVLEJAbkd2Lrkd98kCMZluDWgH7ZDPN5lOxe7yRuFb+7vCmJXZMdgjiiPwuu1aZk8vXFNQNpxwJ8aTQ==');
        xhr.setRequestHeader('X-KGP-DEVTYPE', 'xxx');
        xhr.setRequestHeader('Host', 'portal.office.com');
        xhr.setRequestHeader('Referer', 'https://portal.office.com/SuiteServiceProxy.aspx?upn=luis.valencia%40softwareestrategico.com&suiteServiceReturnUrl=https%3A%2F%2Foutlook.office365.com%2Fowa%2F%3Frealm%3Dsoftwareestrategico.com%26exsvurl%3D1%26ll-cc%3D1033%26modurl%3D0&returnUrl=https%3A%2F%2Foutlook.office365.com%2Fowa%2F%3Frealm%3Dsoftwareestrategico.com%26exsvurl%3D1%26ll-cc%3D1033%26modurl%3D0');
        xhr.setRequestHeader('User-Agent', 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.116 Safari/537.36');
        xhr.setRequestHeader('X-SuiteServiceProxyOrigin', 'https://outlook.office365.com');
    },
    success: function (data) {
        alert(data);
    },
    error: function (xhr, status, error) {
        var err = eval("(" + xhr.responseText + ")");
        alert(err.Message);
    },
    complete: function (data) {
        alert(data);
    },
});

But I always get this error:

enter image description here

What am I missing in this code, to be able to call the service just as CURL and return me the response I need?

like image 702
Luis Valencia Avatar asked Oct 09 '16 19:10

Luis Valencia


People also ask

What are headers in AJAX call?

The headers are additional key-value pairs send along with ajax request using the XMLHttpRequest object. An asynchronous HTTP request to the server by using The ajax() function and by including the header it describes to the server what kind of response it accept.

How does AJAX work in jQuery?

AJAX = Asynchronous JavaScript and XML. In short; AJAX is about loading data in the background and display it on the webpage, without reloading the whole page. Examples of applications using AJAX: Gmail, Google Maps, Youtube, and Facebook tabs. You can learn more about AJAX in our AJAX tutorial.

How do I add a request header in HTML?

Fill out the Create a header fields as follows: In the Name field, enter the name of your header rule (for example, My header ). From the Type menu, select Request, and from the Action menu, select Set. In the Destination field, enter the name of the header affected by the selected action.


3 Answers

Answering your title question is just use the headers settings for $.ajax. For instance:

$.ajax({
    url: 'https://portal.office.com/api/myapps/GetAllApps',
    type: 'GET',
    headers: {
        'Accept': 'application/json',
        'Accept-Encoding': 'gzip, deflate, sdch, br',...
}});

But I don't think that the error is caused because of missing headers. You should explain better the error, if is related with same-origin policy you should take a look on these links Working With and Around the Same-Origin Policy and Cross-Domain requests in Javascript).

like image 88
mfvjunior Avatar answered Oct 03 '22 21:10

mfvjunior


I think you need to provide further information about this error but my first guess would be that the host has No 'Access-Control-Allow-Origin' enabled, and that's why you can do it from console.

You can try running chrome with --insecure or just install the allow-control-origin plugin

like image 32
jonystorm Avatar answered Oct 03 '22 22:10

jonystorm


Can you try with by adding dataType: 'jsonp', crossDomain: true,

like image 23
Rajendar Thatipally Avatar answered Oct 03 '22 20:10

Rajendar Thatipally