I have a target URL from server , and want to send some parameter with specific cookies to it, via Ajax
in Chrome
browser.
Response header of server of target URL (I don't have access to server for any change config or change scripts on it) :
Cache-Control:max-age=600, private, must-revalidate
Cache-Control:no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Connection:keep-alive
Content-Encoding:gzip
Content-Length:6821
Content-Type:text/html
Date:Thu, 16 Nov 2017 22:50:17 GMT
Expires:Thu, 19 Nov 1981 08:52:00 GMT
Pragma:no-cache
Server:Apache
Vary:Accept-Encoding
X-Powered-By:PHP/5.5.38
I started for it!
Firstly I try CURL
for test access. This works so there is a need for server access and there is no need to set a specific header:
curl 'http://www.foo.com/bar.php' \
-H 'Cookie: PHPSESSID=vjjuo34g4gh46ajd471lt8pvn7;' \
--data 'x=1&y=2' --compressed -s
It is only important that the PHPSESSID
cookie is sent.
Response: correct, and has access.
Now I try Ajax and the Chrome browser locally:
$.ajax({
type: "POST",
ContentType: "application/json; charset=UTF-8",
dataType: "json",
data: {x : '1', y: '2'},
url: 'http://www.foo.com/bar.php',
complete: function(data) {
console.log('RESPONSE TEXT>>>>>>>>', data.responseText);
}
});
I receive the following error message:
Failed to load http://www.foo.com/bar.php: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8888' is therefore not allowed access.
I activate the Allow-Control-Allow-Origin
chrome extension and run it again.
This time it works. But there is no access because the cookie is not sent.
Response: incorrect, and has not access.
Add a cookie to the Ajax request header:
beforeSend : function(xhr) {
xhr.setRequestHeader('Cookie', 'PHPSESSID=vjjuo34g4gh46ajd471lt8pvn7;');
},
The cookie is not set and the error appears in the console, Error:
Refused to set unsafe header "Cookie"
Response: incorrect, and has not access.
We add this to the Ajax request to allow us to add a cookie:
xhrFields: {
withCredentials: true,
},
What happens is that my preferred cookie is not set. But the pre-configured cookies are set to my domain for the requested domain.
(We assume that these are the same cookies that are not the issue.)
But this time the following message appears in the console:
Failed to load http://www.foo.com/bar.php: The value of the 'Access-Control-Allow-Origin' header in the response must not be the wildcard '*' when the request's credentials mode is 'include'. Origin 'http://localhost:8888' is therefore not allowed access. The credentials mode of requests initiated by the XMLHttpRequest is controlled by the withCredentials attribute.
Response: correct, but return EMPTY!!! WHY?!
You see that the request contained cookies that were required. But the correct response was not received at data.responseText
and JavaScript.
But it is strange that the Developer Tools> Network> Response
response text is shown correctly.
Why is this happening and how can I finally get the correct response like CURL
request of the first above image?
EDIT:
Output of console.log(data)
command. see here:
Seem the red message was changed in this time! and also seem some time to load response data failed in Response
tab of chrome!