如何使用Ajax调用将Xml数据传递给nodejs?

时间:2019-04-03 08:16:03

标签: javascript node.js ajax xml xml-parsing

我正在尝试使用ajax调用将我的xml数据从javascript注入到nodejs,但没有发生?nodejs和javascript代码如下。 我试图将xml数据转换为json或字符串,但也没有发生

const express = require('express');
const app = express();
var cors = require('cors')
var bodyParser = require('body-parser')
app.use(bodyParser.urlencoded({ extended: false }))

// parse application/json 
app.use(bodyParser.json())


app.get('/', function(req, res){
    res.send('Hello World!');
});

app.get('/xml', function(req, res){
    console.log(req);
    res.send('Hello World!');
});

app.listen(3000, function(){
    console.log(`Example app listening on port 3000!`);
});
$(function () {
        $("#upload").bind("click", function () {
            var regex = /^([a-zA-Z0-9\s_\\.\-:])+(.xml)$/;
            if (regex.test($("#fileUpload").val().toLowerCase())) {
                if (typeof (FileReader) != "undefined") {
                    var reader = new FileReader();
                    reader.onload = function (e) {
                        //console.log(e.target.result);
                        var zzz = e.target.result
                        var xmlDoc = $.parseXML(e.target.result);
                        //var xmlDoc1 = (xmlDoc);
                        console.log(zzz);
                        console.log(xmlDoc);
                       // console.log(xmlDoc1);
                        $("#txtXml").val(xmlDoc);
                        //////////////////////////////////////////ajax call
                        $.ajax({
                            url: 'http://localhost:3000/xml',
                            contentType: "application/text; charset=utf-8",
                            data: {'status': zzz},
                            type: 'GET',
                            cache: false,
                            jsonpCallback: 'callback', // this is not relevant to the POST anymore
                            success: function (data) {
                                var ret = jQuery.parseJSON(data);
                                $('#lblResponse').html(ret.msg);
                                console.log('Success: ')
                            },
                            error: function (xhr, status, error) {
                                console.log('Error: ' + error.message);
                                $('#lblResponse').html('Error connecting to the server.');
                            },
                        });

0 个答案:

没有答案