Answer the question
In order to leave comments, you need to log in
It is not possible to make an ajax request using jquery to the server on node.js, but just a get request - does it work?
I'm trying to make a simple node.js server, and work with it. I smoked manuals, wrote the following text:
server code
var http = require("http");<br>
var url = require("url");<br>
var sys = require("sys");<br>
<br>
function onRequest(request, response) {<br>
var parsed = url.parse(request.url, true);<br>
console.log("Request <" + parsed.pathname + "> received.");<br>
<br>
var key;<br>
console.log("get:");<br>
console.log(sys.inspect(parsed.query));<br>
<br>
console.log("headers");<br>
console.log(sys.inspect(request.headers));<br>
<br>
response.writeHead(200, {'Content-Type': 'text/plain'});<br>
response.write("Success!");<br>
response.end();<br>
}<br>
<br>
http.createServer(onRequest).listen(8888);<br>
<br>
console.log("Server has started.");
<?xml version="1.0" encoding="utf-8"?><br>
<!DOCTYPE html><br>
<html xmlns="http://www.w3.org/1999/xhtml"><br>
<head><br>
<title>Title</title><br>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"/><br>
<script type="text/javascript"><br>
<br>
$(document).ready(function () {<br>
$("div").text("Ready!");<br>
});<br>
<br>
function submitForm(form) {<br>
aj = $.get(<br>
"http://localhost:8888",<br>
{ nnn: form.nnn.value }<br>
);<br>
aj.done(function(data) {<br>
$("pre").text(data);<br>
});<br>
aj.fail(function(errInfo) {<br>
alert(errInfo.statusText);<br>
});<br>
return false;<br>
}<br>
</script><br>
</head><br>
<body><br>
<form onsubmit="return submitForm(this)"><br>
<input name="nnn" type="text"/> <input type="submit"/><br>
</form><br>
<div>Wait!</div><br>
<pre>Result</pre><br>
</body><br>
</html><br>
Answer the question
In order to leave comments, you need to log in
Most likely, the problem is that the node.js server hangs on a different port and, accordingly, you need to make a cross-domain ajax request (you need to add the get-parameter `callback=?` to the requested url).
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question