I'm trying to make a connection to a node server from a page served by an APACHE server but I'm running into issues with the connection. I was getting cross domain errors until I set origin to allow all but now I'm getting a 400 bad request error.
server
var http = require('http');
var io = require('socket.io');
var server = http.createServer(function(request, response){
console.log('Connection');
response.writeHead(200, {'Content-Type': 'text/html'});
//response.write('hello world');
response.end();
});
server.listen(8001);
io.listen(server);
var socket = io.listen(server);
socket.set('origins', '*');
socket.on('connection', function() {
console.log('mooo');
});
client
<script src="https://cdn.socket.io/socket.io-1.3.5.js"></script>
<script src = "http://localhost:8001"> </script>
<script>
var socket = io.connect('http://localhost:8001');
</script>
error:
GET XHR http://localhost:8001/socket.io/ [HTTP/1.1 400 Bad Request 2ms]
Your code is running fine without the following code but Im not running on apache. I have got the output "hello world" in browser (http://localhost:8001/).
var socket = io.listen(server);
io.set('origins', '*');
io.on('connection', function() {
console.log('mooo');
});
How are your accessing the page because 404 is something that your page can not be located by the request.
Related
Here's a NodeJS code snippet I'm debugging. The url is correct, and a GET request to it with Postman returns a 200 OK with the following headers. The response body is a valid JSON string.
Headers (arrows represent column divisors)
Accept-Ranges →bytes
Age →0
Cache-Control →max-age=300
Connection →keep-alive
Content-Encoding →gzip
Content-Length →255
Content-Type →application/json
Date →Tue, 24 Jan 2017 22:37:28 GMT
Expires →Tue, 24 Jan 2017 17:47:43 GMT
Last-Modified →Tue, 24 Jan 2017 01:03:08 GMT
Server →nginx
Vary →Accept-Encoding
Via →1.1 varnish
X-Cache →HIT
X-Cache-Hits →1
X-Served-By →cache-yul8926-YUL
X-Timer →S1485297448.259768,VS0,VE89
The problem:
I've discovered that res.on('data', function(chunk) { never gets called which causes body to remain empty.
When res.on('end', function() { is called body still has length = 0. Any ideas why the data callback is not getting called?
http.get(url, function(res) {
var body = '';
res.on('data', function(chunk) {
body += chunk;
});
res.on('end', function() {
var data = JSON.parse(body);
cb(data, undefined);
});
}).on('error', function(err) {
console.log("Something went wrong");
console.log(err);
});
Also worth nothing, cb is a callback function defined outside of this snippet.
After the lovely advice from jfriend00 I discovered that res.statusCode was 301 (Moved Permanently). Seems that the website was redirecting to the same URL but with https protocol instead of http.
When testing the REST API with Postman it did not mention the fact the request was redirected. Postman displayed a 200 OK response (???) According to the Postman Docs it will default to following redirects silently.
The solution:
I replaced all instances of http with https and it worked great.
https.get(url, function(res) {
var body = '';
res.on('data', function(chunk) {
body += chunk;
});
res.on('end', function() {
var data = JSON.parse(body);
cb(data, undefined);
});
}).on('error', function(err) {
console.log("Something went wrong");
console.log(err);
});
I'm using XMLHttpRequest() to send an xml request to asp classic page. The same request returns the expected response (response = "Response received") when server.asp is on localhost, but not when it is on my godaddy website.
When I googled I found that it's because of crossdomain, but I couldn't find a solution for it.
I tried the same with $.ajax with same result.
Javascript:
var xmlhttp = new XMLHttpRequest();
xmlhttp.open("POST", "http://www.myserver.com/server.asp", false);
xmlhttp.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
xmlhttp.send("<xml/>");
var response = xmlhttp.responseText;
server.asp
Response.Write ("Response received")
I am very new to ajax development, I am trying to use xhr to get and post data,the problem is when I use port based requests?
here is my working code and not working codes
$.ajax({
url : "login.php",
type : "post",
data : {
userProfile : JSON.stringify(data)
},
success : handleDBResponse,
error : function(jqXHR, textStatus,errorThrown) {
console.log("The following error occured: "+ textStatus,errorThrown);
},
complete : function() {
// console.log("user authentication
// successful.")
}
});
this works good, but when I am using native xhr with url:port getting no response.
function reqListener () {
console.log(this.responseText);
};
var oReq = new XMLHttpRequest();
oReq.onload = reqListener;
oReq.open("get", "http://www.domain.com:443/akorp.css", true);
oReq.send();
It's not working, I was debugged and I found request status is cancelled.
.htaccess file included
Access-Control-Allow-Origin:*
still I am getting the error
www.domain.com:443 not allowed by www.domain.com Access-Control-Allow-Origin etc..
what are the possible causes for this error and how to properly send request to port?
443 is the HTTPS port. Perhaps you should try an HTTPS URL instead of forcing the port.
I'm not sure I want to know why you're pulling a CSS file from somebody else's serer with xhr.
I'm doing straight-to-S3 multipart file upload via AJAX. Everything works fine under all browsers but IE.
S3 requires an Authorization HTTP header in each POST request which contains the signature of the file slice being uploaded.
It appears IE strips out this header from the request, yielding a 403 response.
What's more funny is that IE does not strip another custom S3 header: x-amz-date.
Any idea how I can force the 'Authorization' header in?
As requested, here is my code :
initiateUpload: function() {
var response = this.sign({method:'POST', path: this.key + '?uploads'});
this.request({
method: 'POST',
url: response.url,
headers: {
'x-amz-date': response.date,
'Authorization': response.signature
},
onLoad: this.uploadParts.bind(this)
});
},
request: function(params){
var xhr = new XMLHttpRequest();
if (params.onLoad) xhr.addEventListener("load", params.onLoad, false);
if (params.onUploadStart) xhr.upload.onloadstart = params.onUploadStart;
if (params.onUploadProgress) xhr.upload.onprogress = params.onUploadProgress;
xhr.open(params.method, params.url, true);
for (h in params.headers)
xhr.setRequestHeader(h, params.headers[h]);
xhr.send(params.body);
},
If I do an XMLHttpRequest request to some server and that server doesn't return anything (it just hangs), will the XMLHttpRequest eventually time out?
Try:
xhr.timeout = 10000;
xhr.ontimeout = function(){ alert('Timeout!')};
reference: