A
A
Anton Anton2013-02-01 22:17:27
JavaScript
Anton Anton, 2013-02-01 22:17:27

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.");

If you just open localhost:8888, then everything works, headers are displayed in the console, success appears in the browser.

But I'm trying to get the same thing through ajax using node.js - it gives an error. Form code:
html content
<?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>


At the same time, the console displays that there is a request, its parameters, the server does not crash with an error, it continues to accept requests.
In firebug, this is (8 bytes, this is “Success!”, As I understand it. At least if you change the returned message, this number changes accordingly):
pictureplasma-desktopry2248.png
How to make it work?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
Y
Yuri Shikanov, 2013-02-01
@Fragster

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).

M
max_mara, 2013-02-01
@max_mara

Maybe your ajax is hoping to receive json, but is receiving html?

K
Karen Kratyan, 2013-02-01
@kratkar

Have you tried other versions of jquery?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question