• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

[Javascript] Failed XMLHttpRequest

Status
Not open for further replies.

Chaosy

Tutorial Reviewer
Level 40
Joined
Jun 9, 2011
Messages
13,182
UPDATE: It works if I put invalid information. So if I change one character in the password, the callback works.


I am currently working on a server side application which is basically supposed to Skyscrape a few test websites.

Now, it MIGHT be that I am using the wrong link - which I doubt.

I tracked down the following by logging in:
7bb656322a97eb1d713fd708207e0557.png


I am supposed to make a replica of that request.

JavaScript:
static SendP(URL, toSend, callback) {
    let req = new XMLHttpRequest();
    req.addEventListener("load", callback);
    req.open("POST", URL);
    req.setRequestHeader("Content-Type", "application/json");
    req.send(JSON.stringify(toSend));
    console.log(toSend);
}
Ajax.SendP("http://vhost3.lnu.se:20080/dinner/login/", {username: "censored", password: "censored", submit: "login"}, onLogin);

the "console.log(toSend)" displays the correct value but the callback (onLogin) never gets called.
And I need that to work since it is supposed to return a session cookie.
 
Last edited:
Level 29
Joined
Jul 29, 2007
Messages
5,174
As you can see in the HTTP headers, you actually want the application/x-www-form-urlencoded mime, not json.
JSON.stringify is also not the same as url-encoding, check the output!

You can send it manually like you do, or you might consider using a FormData object.

Whether that's the actual issue, I don't know, didn't touch HTTP in a while, but you should definitely check how your request actually looks, and compare it to the valid one.

As a general rule, you might also want to add error and progress handlers, even if only so you yourself could know what's happening while testing.
 
Last edited:

Chaosy

Tutorial Reviewer
Level 40
Joined
Jun 9, 2011
Messages
13,182
I changed the method entirely, I now use request instead.
This solved the problem.

However, a small other issue that followed.
I do not know how to fully read the information.
JavaScript:
function onLogin(err, resp, body) {
    console.log(body);
    console.log(resp);
    if(err) {
        console.log(err);
    }
}

Body simply says "Redirecting to page xxxx" (which does not work obviously since I am running through code and not from the browser)

So, I need a session cookie. Which is hidden somewhere in the "resp" variable.

friday
{ status: 1, day: '05', time: '18:00', movie: '01' }
Found. Redirecting to login/booking
Unauthorized

vagrant@vagrant-ubuntu-trusty-32:/vagrant$ node index
friday
{ status: 1, day: '05', time: '18:00', movie: '01' }
Found. Redirecting to login/booking
IncomingMessage {
_readableState:
ReadableState {
objectMode: false,
highWaterMark: 16384,
buffer: BufferList { head: null, tail: null, length: 0 },
length: 0,
pipes: null,
pipesCount: 0,
flowing: true,
ended: true,
endEmitted: true,
reading: false,
sync: true,
needReadable: false,
emittedReadable: false,
readableListening: false,
resumeScheduled: false,
defaultEncoding: 'utf8',
ranOut: false,
awaitDrain: 0,
readingMore: false,
decoder: null,
encoding: null },
readable: false,
domain: null,
_events:
{ end: [ [Function: responseOnEnd], [Function] ],
close: [ [Function], [Function] ],
data: [Function],
error: [Function] },
_eventsCount: 4,
_maxListeners: undefined,
socket:
Socket {
connecting: false,
_hadError: false,
_handle: null,
_parent: null,
_host: 'vhost3.lnu.se',
_readableState:
ReadableState {
objectMode: false,
highWaterMark: 16384,
buffer: [Object],
length: 0,
pipes: null,
pipesCount: 0,
flowing: true,
ended: false,
endEmitted: false,
reading: true,
sync: false,
needReadable: true,
emittedReadable: false,
readableListening: false,
resumeScheduled: false,
defaultEncoding: 'utf8',
ranOut: false,
awaitDrain: 0,
readingMore: false,
decoder: null,
encoding: null },
readable: false,
domain: null,
_events:
{ end: [Object],
finish: [Function: onSocketFinish],
_socketEnd: [Function: onSocketEnd],
free: [Function: onFree],
close: [Object],
agentRemove: [Function: onRemove],
drain: [Function: ondrain],
error: [Function: socketErrorListener] },
_eventsCount: 8,
_maxListeners: undefined,
_writableState:
WritableState {
objectMode: false,
highWaterMark: 16384,
needDrain: false,
ending: true,
ended: true,
finished: true,
decodeStrings: false,
defaultEncoding: 'utf8',
length: 0,
writing: false,
corked: 0,
sync: false,
bufferProcessing: false,
onwrite: [Function: bound onwrite],
writecb: null,
writelen: 0,
bufferedRequest: null,
lastBufferedRequest: null,
pendingcb: 0,
prefinished: true,
errorEmitted: false,
bufferedRequestCount: 0,
corkedRequestsFree: [Object] },
writable: false,
allowHalfOpen: false,
destroyed: true,
_bytesDispatched: 186,
_sockname: null,
_pendingData: null,
_pendingEncoding: '',
server: null,
_server: null,
parser: null,
_httpMessage:
ClientRequest {
domain: null,
_events: [Object],
_eventsCount: 5,
_maxListeners: undefined,
output: [],
outputEncodings: [],
outputCallbacks: [],
outputSize: 0,
writable: true,
_last: true,
upgrading: false,
chunkedEncoding: false,
shouldKeepAlive: false,
useChunkedEncodingByDefault: true,
sendDate: false,
_removedHeader: [Object],
_contentLength: null,
_hasBody: true,
_trailer: '',
finished: true,
_headerSent: true,
socket: [Circular],
connection: [Circular],
_header: 'POST /dinner/login HTTP/1.1\r\nhost: vhost3.lnu.se:20080\r\ncontent-type: application/x-www-form-urlencoded\r\ncontent-length: 40\r\nConnection: close\r\n\r\n',
_headers: [Object],
_headerNames: [Object],
_onPendingData: null,
agent: [Object],
socketPath: undefined,
timeout: undefined,
method: 'POST',
path: '/dinner/login',
_ended: true,
parser: null,
res: [Circular] },
read: [Function],
_consuming: true,
_idleNext: null,
_idlePrev: null,
_idleTimeout: -1 },
connection:
Socket {
connecting: false,
_hadError: false,
_handle: null,
_parent: null,
_host: 'vhost3.lnu.se',
_readableState:
ReadableState {
objectMode: false,
highWaterMark: 16384,
buffer: [Object],
length: 0,
pipes: null,
pipesCount: 0,
flowing: true,
ended: false,
endEmitted: false,
reading: true,
sync: false,
needReadable: true,
emittedReadable: false,
readableListening: false,
resumeScheduled: false,
defaultEncoding: 'utf8',
ranOut: false,
awaitDrain: 0,
readingMore: false,
decoder: null,
encoding: null },
readable: false,
domain: null,
_events:
{ end: [Object],
finish: [Function: onSocketFinish],
_socketEnd: [Function: onSocketEnd],
free: [Function: onFree],
close: [Object],
agentRemove: [Function: onRemove],
drain: [Function: ondrain],
error: [Function: socketErrorListener] },
_eventsCount: 8,
_maxListeners: undefined,
_writableState:
WritableState {
objectMode: false,
highWaterMark: 16384,
needDrain: false,
ending: true,
ended: true,
finished: true,
decodeStrings: false,
defaultEncoding: 'utf8',
length: 0,
writing: false,
corked: 0,
sync: false,
bufferProcessing: false,
onwrite: [Function: bound onwrite],
writecb: null,
writelen: 0,
bufferedRequest: null,
lastBufferedRequest: null,
pendingcb: 0,
prefinished: true,
errorEmitted: false,
bufferedRequestCount: 0,
corkedRequestsFree: [Object] },
writable: false,
allowHalfOpen: false,
destroyed: true,
_bytesDispatched: 186,
_sockname: null,
_pendingData: null,
_pendingEncoding: '',
server: null,
_server: null,
parser: null,
_httpMessage:
ClientRequest {
domain: null,
_events: [Object],
_eventsCount: 5,
_maxListeners: undefined,
output: [],
outputEncodings: [],
outputCallbacks: [],
outputSize: 0,
writable: true,
_last: true,
upgrading: false,
chunkedEncoding: false,
shouldKeepAlive: false,
useChunkedEncodingByDefault: true,
sendDate: false,
_removedHeader: [Object],
_contentLength: null,
_hasBody: true,
_trailer: '',
finished: true,
_headerSent: true,
socket: [Circular],
connection: [Circular],
_header: 'POST /dinner/login HTTP/1.1\r\nhost: vhost3.lnu.se:20080\r\ncontent-type: application/x-www-form-urlencoded\r\ncontent-length: 40\r\nConnection: close\r\n\r\n',
_headers: [Object],
_headerNames: [Object],
_onPendingData: null,
agent: [Object],
socketPath: undefined,
timeout: undefined,
method: 'POST',
path: '/dinner/login',
_ended: true,
parser: null,
res: [Circular] },
read: [Function],
_consuming: true,
_idleNext: null,
_idlePrev: null,
_idleTimeout: -1 },
httpVersionMajor: 1,
httpVersionMinor: 1,
httpVersion: '1.1',
complete: true,
headers:
{ server: 'nginx/1.10.1',
date: 'Fri, 27 Jan 2017 10:55:11 GMT',
'content-type': 'text/plain; charset=utf-8',
'content-length': '35',
connection: 'close',
'x-powered-by': 'Express',
location: 'login/booking',
vary: 'Accept',
'set-cookie': [ 'qi88ZyOC0BVAZDoD6yKSpSessionId=s%3AV6vGBlXevquWzQLWyXrRubICPr88WG6q.t6qR%2FJkMQNxYSbVJjgAiLZIavc2FCRJKRQXO8FWGnLA; Path=/; Expires=Sat, 28 Jan 2017 10:55:11 GMT; HttpOnly' ] },
rawHeaders:
[ 'Server',
'nginx/1.10.1',
'Date',
'Fri, 27 Jan 2017 10:55:11 GMT',
'Content-Type',
'text/plain; charset=utf-8',
'Content-Length',
'35',
'Connection',
'close',
'X-Powered-By',
'Express',
'Location',
'login/booking',
'Vary',
'Accept',
'set-cookie',
'qi88ZyOC0BVAZDoD6yKSpSessionId=s%3AV6vGBlXevquWzQLWyXrRubICPr88WG6q.t6qR%2FJkMQNxYSbVJjgAiLZIavc2FCRJKRQXO8FWGnLA; Path=/; Expires=Sat, 28 Jan 2017 10:55:11 GMT; HttpOnly' ],
trailers: {},
rawTrailers: [],
upgrade: false,
url: '',
method: null,
statusCode: 302,
statusMessage: 'Found',
client:
Socket {
connecting: false,
_hadError: false,
_handle: null,
_parent: null,
_host: 'vhost3.lnu.se',
_readableState:
ReadableState {
objectMode: false,
highWaterMark: 16384,
buffer: [Object],
length: 0,
pipes: null,
pipesCount: 0,
flowing: true,
ended: false,
endEmitted: false,
reading: true,
sync: false,
needReadable: true,
emittedReadable: false,
readableListening: false,
resumeScheduled: false,
defaultEncoding: 'utf8',
ranOut: false,
awaitDrain: 0,
readingMore: false,
decoder: null,
encoding: null },
readable: false,
domain: null,
_events:
{ end: [Object],
finish: [Function: onSocketFinish],
_socketEnd: [Function: onSocketEnd],
free: [Function: onFree],
close: [Object],
agentRemove: [Function: onRemove],
drain: [Function: ondrain],
error: [Function: socketErrorListener] },
_eventsCount: 8,
_maxListeners: undefined,
_writableState:
WritableState {
objectMode: false,
highWaterMark: 16384,
needDrain: false,
ending: true,
ended: true,
finished: true,
decodeStrings: false,
defaultEncoding: 'utf8',
length: 0,
writing: false,
corked: 0,
sync: false,
bufferProcessing: false,
onwrite: [Function: bound onwrite],
writecb: null,
writelen: 0,
bufferedRequest: null,
lastBufferedRequest: null,
pendingcb: 0,
prefinished: true,
errorEmitted: false,
bufferedRequestCount: 0,
corkedRequestsFree: [Object] },
writable: false,
allowHalfOpen: false,
destroyed: true,
_bytesDispatched: 186,
_sockname: null,
_pendingData: null,
_pendingEncoding: '',
server: null,
_server: null,
parser: null,
_httpMessage:
ClientRequest {
domain: null,
_events: [Object],
_eventsCount: 5,
_maxListeners: undefined,
output: [],
outputEncodings: [],
outputCallbacks: [],
outputSize: 0,
writable: true,
_last: true,
upgrading: false,
chunkedEncoding: false,
shouldKeepAlive: false,
useChunkedEncodingByDefault: true,
sendDate: false,
_removedHeader: [Object],
_contentLength: null,
_hasBody: true,
_trailer: '',
finished: true,
_headerSent: true,
socket: [Circular],
connection: [Circular],
_header: 'POST /dinner/login HTTP/1.1\r\nhost: vhost3.lnu.se:20080\r\ncontent-type: application/x-www-form-urlencoded\r\ncontent-length: 40\r\nConnection: close\r\n\r\n',
_headers: [Object],
_headerNames: [Object],
_onPendingData: null,
agent: [Object],
socketPath: undefined,
timeout: undefined,
method: 'POST',
path: '/dinner/login',
_ended: true,
parser: null,
res: [Circular] },
read: [Function],
_consuming: true,
_idleNext: null,
_idlePrev: null,
_idleTimeout: -1 },
_consuming: true,
_dumped: false,
req:
ClientRequest {
domain: null,
_events:
{ socket: [Object],
response: [Function: bound ],
error: [Function: bound ],
drain: [Function],
prefinish: [Function: requestOnPrefinish] },
_eventsCount: 5,
_maxListeners: undefined,
output: [],
outputEncodings: [],
outputCallbacks: [],
outputSize: 0,
writable: true,
_last: true,
upgrading: false,
chunkedEncoding: false,
shouldKeepAlive: false,
useChunkedEncodingByDefault: true,
sendDate: false,
_removedHeader: { 'content-length': false },
_contentLength: null,
_hasBody: true,
_trailer: '',
finished: true,
_headerSent: true,
socket:
Socket {
connecting: false,
_hadError: false,
_handle: null,
_parent: null,
_host: 'vhost3.lnu.se',
_readableState: [Object],
readable: false,
domain: null,
_events: [Object],
_eventsCount: 8,
_maxListeners: undefined,
_writableState: [Object],
writable: false,
allowHalfOpen: false,
destroyed: true,
_bytesDispatched: 186,
_sockname: null,
_pendingData: null,
_pendingEncoding: '',
server: null,
_server: null,
parser: null,
_httpMessage: [Circular],
read: [Function],
_consuming: true,
_idleNext: null,
_idlePrev: null,
_idleTimeout: -1 },
connection:
Socket {
connecting: false,
_hadError: false,
_handle: null,
_parent: null,
_host: 'vhost3.lnu.se',
_readableState: [Object],
readable: false,
domain: null,
_events: [Object],
_eventsCount: 8,
_maxListeners: undefined,
_writableState: [Object],
writable: false,
allowHalfOpen: false,
destroyed: true,
_bytesDispatched: 186,
_sockname: null,
_pendingData: null,
_pendingEncoding: '',
server: null,
_server: null,
parser: null,
_httpMessage: [Circular],
read: [Function],
_consuming: true,
_idleNext: null,
_idlePrev: null,
_idleTimeout: -1 },
_header: 'POST /dinner/login HTTP/1.1\r\nhost: vhost3.lnu.se:20080\r\ncontent-type: application/x-www-form-urlencoded\r\ncontent-length: 40\r\nConnection: close\r\n\r\n',
_headers:
{ host: 'vhost3.lnu.se:20080',
'content-type': 'application/x-www-form-urlencoded',
'content-length': 40 },
_headerNames:
{ host: 'host',
'content-type': 'content-type',
'content-length': 'content-length' },
_onPendingData: null,
agent:
Agent {
domain: null,
_events: [Object],
_eventsCount: 1,
_maxListeners: undefined,
defaultPort: 80,
protocol: 'http:',
options: [Object],
requests: {},
sockets: [Object],
freeSockets: {},
keepAliveMsecs: 1000,
keepAlive: false,
maxSockets: Infinity,
maxFreeSockets: 256 },
socketPath: undefined,
timeout: undefined,
method: 'POST',
path: '/dinner/login',
_ended: true,
parser: null,
res: [Circular] },
request:
Request {
domain: null,
_events:
{ error: [Function: bound ],
complete: [Function: bound ],
pipe: [Function],
data: [Function],
end: [Function] },
_eventsCount: 5,
_maxListeners: undefined,
method: 'POST',
uri:
Url {
protocol: 'http:',
slashes: true,
auth: null,
host: 'vhost3.lnu.se:20080',
port: '20080',
hostname: 'vhost3.lnu.se',
hash: null,
search: null,
query: null,
pathname: '/dinner/login',
path: '/dinner/login',
href: 'http://vhost3.lnu.se:20080/dinner/login' },
callback: [Function],
readable: true,
writable: true,
explicitMethod: true,
_qs:
Querystring {
request: [Circular],
lib: [Object],
useQuerystring: undefined,
parseOptions: {},
stringifyOptions: [Object] },
_auth:
Auth {
request: [Circular],
hasAuth: false,
sentAuth: false,
bearerToken: null,
user: null,
pass: null },
_oauth: OAuth { request: [Circular], params: null },
_multipart:
Multipart {
request: [Circular],
boundary: '2968a595-0c4c-4f44-8cb8-d91b3ee3ea9f',
chunked: false,
body: null },
_redirect:
Redirect {
request: [Circular],
followRedirect: true,
followRedirects: true,
followAllRedirects: false,
followOriginalHttpMethod: false,
allowRedirect: [Function],
maxRedirects: 10,
redirects: [],
redirectsFollowed: 0,
removeRefererHeader: false },
_tunnel:
Tunnel {
request: [Circular],
proxyHeaderWhiteList: [Object],
proxyHeaderExclusiveList: [] },
headers:
{ 'content-type': 'application/x-www-form-urlencoded',
'content-length': 40 },
setHeader: [Function],
hasHeader: [Function],
getHeader: [Function],
removeHeader: [Function],
localAddress: undefined,
pool: {},
dests: [],
__isRequestRequest: true,
_callback: [Function: onLogin],
proxy: null,
tunnel: false,
setHost: true,
originalCookieHeader: undefined,
_disableCookies: true,
_jar: undefined,
port: '20080',
host: 'vhost3.lnu.se',
body: 'username=zeke&password=coys&submit=login',
path: '/dinner/login',
httpModule:
{ IncomingMessage: [Object],
OutgoingMessage: [Object],
METHODS: [Object],
Agent: [Object],
globalAgent: [Object],
ServerResponse: [Object],
STATUS_CODES: [Object],
_connectionListener: [Function: connectionListener],
Server: [Object],
createServer: [Function: createServer],
ClientRequest: [Object],
request: [Function: request],
get: [Function: get] },
agentClass: { [Function: Agent] super_: [Object], defaultMaxSockets: Infinity },
agent:
Agent {
domain: null,
_events: [Object],
_eventsCount: 1,
_maxListeners: undefined,
defaultPort: 80,
protocol: 'http:',
options: [Object],
requests: {},
sockets: [Object],
freeSockets: {},
keepAliveMsecs: 1000,
keepAlive: false,
maxSockets: Infinity,
maxFreeSockets: 256 },
_started: true,
href: 'http://vhost3.lnu.se:20080/dinner/login',
req:
ClientRequest {
domain: null,
_events: [Object],
_eventsCount: 5,
_maxListeners: undefined,
output: [],
outputEncodings: [],
outputCallbacks: [],
outputSize: 0,
writable: true,
_last: true,
upgrading: false,
chunkedEncoding: false,
shouldKeepAlive: false,
useChunkedEncodingByDefault: true,
sendDate: false,
_removedHeader: [Object],
_contentLength: null,
_hasBody: true,
_trailer: '',
finished: true,
_headerSent: true,
socket: [Object],
connection: [Object],
_header: 'POST /dinner/login HTTP/1.1\r\nhost: vhost3.lnu.se:20080\r\ncontent-type: application/x-www-form-urlencoded\r\ncontent-length: 40\r\nConnection: close\r\n\r\n',
_headers: [Object],
_headerNames: [Object],
_onPendingData: null,
agent: [Object],
socketPath: undefined,
timeout: undefined,
method: 'POST',
path: '/dinner/login',
_ended: true,
parser: null,
res: [Circular] },
ntick: true,
response: [Circular],
originalHost: 'vhost3.lnu.se:20080',
originalHostHeaderName: 'host',
responseContent: [Circular],
_destdata: true,
_ended: true,
_callbackCalled: true },
toJSON: [Function: responseToJSON],
caseless:
Caseless {
dict:
{ server: 'nginx/1.10.1',
date: 'Fri, 27 Jan 2017 10:55:11 GMT',
'content-type': 'text/plain; charset=utf-8',
'content-length': '35',
connection: 'close',
'x-powered-by': 'Express',
location: 'login/booking',
vary: 'Accept',
'set-cookie': [Object] } },
read: [Function],
body: 'Found. Redirecting to login/booking' }


There are a few set-cookie in there, pretty sure I can use one of them. However I cannot seem to understand how to get them.
 
Level 29
Joined
Jul 29, 2007
Messages
5,174
qi88ZyOC0BVAZDoD6yKSpSessionId=s%3AV6vGBlXevquWzQLWyXrRubICPr88WG6q.t6qR%2FJkMQNxYSbVJjgAiLZIavc2FCRJKRQXO8FWGnLA

You can see there the key-value pair. No clue why they added all that extra gibrish, you'll need to figure it out, this depends on the site, not on whatever JS code you have.
 
Level 29
Joined
Jul 29, 2007
Messages
5,174
On a side note, I am kind of surprised you can see that cookie. It is set with HttpOnly, which means it's not supposed to be visible to JS. This is the change that was made to the Hive's chat to avoid people like me stealing people's sessions (after I suggested the change, that is).
 
Status
Not open for further replies.
Top