使用form方法的Ajax作为node.js中的post

时间:2016-12-30 10:22:44

标签: javascript ajax node.js

我在使用node.js的表单中遇到Ajax问题,我正在开发一个简单的node.js货币转换器应用程序,并使用Ajax在前端(HTML)上填充数据。但是它无法正常工作,任何帮助都会受到重视。感谢。

1 个答案:

答案 0 :(得分:1)

1。前端

更改此

xmlhttp.open("GET","http://localhost:9099/", true); 

xmlhttp.open("POST","http://localhost:9099/", true); 

,因为您的后端服务器接受POST来获取答案。

2。后端

从底部移除response.endresponse.writeHead,然后将其移至您计算store的位置。

您的最终代码:

http.createServer(function(request, response) {
  switch (request.method) {
    case 'POST':
      if (request.url === "/") {
        var requestBody = '';
        request.on('data', function(data) {
          requestBody += data;
          if (requestBody.length > 1e7) {
            response.writeHead(413, {
              'Content-Type': 'text/plain'
            });
            response.end('Request Entity is too large');
          }
        });

        request.on('end', function(data) {
          console.log(requestBody);
          var formData = qs.parse(requestBody);

          var requestBofy = '';

          // I renamed the callback parameter to response2
          https.get('https://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml?8f65cd5d1af1727c40d2a89a31c6a0f1', function(response2) {
            if (response2.statusCode >= 200 && response2.statusCode < 400) {
              response2.on('data', function(data_) {
                requestBofy += data_.toString();
              });
              response2.on('end', function() {
                console.log(requestBofy);
                parser.parseString(requestBofy, function(err, result) {
                  console.log('FINISHED', err, result);

                  var xml = requestBofy;

                  var parseString = require('xml2js').parseString;
                  parseString(xml, function(err, result) {
                    var jFile = JSON.stringify(result);

                    var parsedResponse = JSON.parse(jFile);
                    var rateHUF = parsedResponse['gesmes:Envelope']['Cube'][0]['Cube'][0]['Cube'][6]['$'].rate;
                    var rateINR = parsedResponse['gesmes:Envelope']['Cube'][0]['Cube'][0]['Cube'][22]['$'].rate;
                    var store = 'No value';
                    if (formData.vSelectedValue == 'HUF' && formData.vSelectedValue2 == 'INR') {
                      store = Math.round(formData.vFirstNo * (rateINR / rateHUF));
                    } else {
                      store = Math.round(formData.vFirstNo * (rateHUF / rateINR));
                    }

                    // Your response should end here
                    response.writeHead(200, {
                      "Content-Type": "text/html"
                    });
                    response.end('Your Answer: ' + store);

                  });

                });
              });
            }
          });

        });
      } else {
        response.writeHead(404, {
          'Content-Type': 'text/plain'
        });
        response.end('404 - Page not found');
      }

      break;
    case 'GET':
      if (request.url === "/") {

        getFileContent(response, 'public/home.html', 'text/html');

      } else {
        response.writeHead(404, {
          'Content-Type': 'text/plain'
        });
        response.end('404 - Page not found');

      }
      break;

    default:
      response.writeHead(404, {
        'Content-Type': 'text/plain'
      });
      response.end('404 - Page not found');

  }

}).listen(9099);