Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/397.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
javascript post xml控制器读取xml_Javascript_Jquery_Xml_Post_Controller - Fatal编程技术网

javascript post xml控制器读取xml

javascript post xml控制器读取xml,javascript,jquery,xml,post,controller,Javascript,Jquery,Xml,Post,Controller,我想将一个XML字符串作为POST从javascript发送到服务器。XML具有以下结构: <?xml version="1.0" encoding="UTF-8"?> <Devices> <Device> <Id>212121</Id> <Accuracy>3</Accuracy> <BatteryVolts>12.34</Batter

我想将一个XML字符串作为POST从javascript发送到服务器。XML具有以下结构:

  <?xml version="1.0" encoding="UTF-8"?>
   <Devices>
     <Device>
        <Id>212121</Id>
      <Accuracy>3</Accuracy>
     <BatteryVolts>12.34</BatteryVolts>
   </Device>
  <Device>
   <Id>212122</Id>
    <Accuracy>5</Accuracy>
    <BatteryVolts>12.14</BatteryVolts>
 </Device>
</Devices>

一旦我将xml放入控制器,我就可以解析它,没有问题。请帮忙,谢谢

看起来您的服务器希望xml作为请求主体,所以请尝试设置contentType:application/xml,如果您能提供如何使用这段代码:这也是javascript。它返回的“defineClass”未定义。ISBN 0-596-10199-6 David Flanagan第9章-示例:defineClass实用程序方法您也可以在此处找到函数我为这段庞大的代码道歉。同时,它是纯javaScript。在代码的最后,您可以看到一个工作代码示例,其中需要替换服务器地址和消息内容,以及替换处理服务器响应的函数。如果您愿意,我可以重写任务管理器,使其与IE4等旧浏览器更加兼容。
var defineClass = function () {
    var inheritance = function inheritance() { };
    return function defineClass(data) {
        var classname = data.name,
            superclass = data.extend || Object,
            constructor = data.construct || function () { },
            methods = data.methods || {},
            statics = data.statics || {},
            borrows,
            provides;
        if (!data.borrows) {
            borrows = [];
        }
        else {
            if (data.borrows instanceof Array) {
                borrows = data.borrows;
            }
            else {
                borrows = [data.borrows];
            };
        };
        if (!data.provides) {
            provides = [];
        }
        else {
            if (data.provides instanceof Array) {
                provides = data.provides;
            }
            else {
                provides = [data.provides];
            };
        };
        inheritance.prototype = superclass.prototype;
        var proto = new inheritance();
        for (var i = 0; i < borrows.length; i++) {
            var c = borrows[i];
            for (var p in c.prototype) {
                if (typeof c.prototype[p] != "function") continue;
                proto[p] = c.prototype[p];
            }
        }
        for (var p in methods) {
            proto[p] = methods[p];
        };
        proto.constructor = constructor;
        constructor.superclass = superclass.prototype;
        if (classname) {
            proto.classname = classname;
        };
        for (var i = 0; i < provides.length; i++) {
            var c = provides[i];
            for (var p in c.prototype) {
                if (typeof c.prototype[p] != "function") {
                    continue;
                };
                if (p == "constructor" || p == "superclass") {
                    continue;
                };
                if (p in proto && typeof proto[p] == "function" && proto[p].length == c.prototype[p].length) {
                    continue;
                };
                throw new Error("Class " + classname + " are not provided method " + c.classname + "." + p);
            };
        };
        constructor.prototype = proto;
        for (var p in statics) {
            constructor[p] = statics[p];
        };
        return constructor;
    }
}();
var EndPoint = (function () {
    return defineClass({
        name: "EndPoint",
        construct: function (urlObj) {
            var myUrl,
                myScheme,
                myDefaultScheme = "http",
                myLogin,
                myPassword,
                myHost,
                myDefaultHost = "localhost",
                myIP,
                myDefaultIP = "127.0.0.1",
                myPort,
                myDefaultPort = "80",
                myPath,
                myDefaultPath = "/",
                myOptions,
                myAnchor;
            var self = this;
            var parse_url = /^(?:([A-Za-z]+):)?(\/{0,3})(?:([A-Za-z]+):([A-Za-z]+)@)?([0-9.\-A-Za-z]+)(?::(\d+))?(?:\/([^?#]*))?(?:\?([^#]*))?(?:#(.*))?$/;
            var result = parse_url.exec(urlObj);
            var names = ['url', 'scheme', 'slash', 'login', 'password', 'host', 'port', 'path', 'query', 'hash'];
            var i;
            for (i = 0; i < names.length; i += 1) {
                switch (names[i]) {
                    case 'url':
                        myUrl = result[i];
                        break;
                    case 'scheme':
                        myScheme = result[i];
                        break;
                    case 'slash':
                        break;
                    case 'login':
                        myLogin = result[i];
                        break;
                    case 'password':
                        myPassword = result[i];
                        break;
                    case 'host':
                        myHost = result[i];
                        break;
                    case 'port':
                        myPort = result[i];
                        break;
                    case 'path':
                        myPath = result[i];
                        break;
                    case 'query':
                        myOptions = result[i];
                        break;
                    case 'hash':
                        myAnchor = result[i];
                        break;
                }
            }
            this.scheme = myScheme;
            this.login = myLogin;
            this.password = myPassword;
            this.host = myHost;
            this.ip = myIP;
            this.port = myPort;
            this.path = myPath;
            this.options = myOptions;
            this.anchor = myAnchor;
            this.url = myUrl;
        },
        methods: {
            ValidateIP: function (ip) {
                return /^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/.test(ip);
            },
            ValidateUrl: function (url) {
                return /^(http|ftp|https):\/\/[\w-]+(\.[\w-]+)+([\w.,@?^=%&amp;:\/~+#-]*[\w@?^=%&amp;\/~+#-])?/.test(url);
            },
            ValidateShortUrl: function (url) {
                if (/^\/\/[\w-]+(\.[\w-]+)+([\w.,@?^=%&amp;:\/~+#-]*[\w@?^=%&amp;\/~+#-])?/.test(url)) {
                    return true;
                };
                if (/^[\w-]+(\.[\w-]+)+([\w.,@?^=%&amp;:\/~+#-]*[\w@?^=%&amp;\/~+#-])?/.test(url)) {
                    return true;
                }
                return false;
            }
        }
    })
}());
var QueryType = function QueryType() {
    if (!(this instanceof QueryType)) {
        return new QueryType(arguments[0]);
    };
    this.endPoint = arguments[0]["endPoint"];
    this.envelope = arguments[0]["envelope"];
    this.type = arguments[0]["type"];
    this.headers = arguments[0]["headers"];
    this.callback_success = arguments[0]["callback_success"];
    this.callback_failure = arguments[0]["callback_failure"];
    this.timeout = arguments[0]["timeout"];
    this.username = arguments[0]["username"];
    this.password = arguments[0]["password"];
};
var TaskManager = function () {
    if (!(this instanceof TaskManager)) {
        return new TaskManager();
    };
    var instance;
    TaskManager = function () {
        return instance;
    };
    TaskManager.prototype = this;
    instance = new TaskManager();
    instance.constructor = TaskManager;
    function Request() {
        if (!(this instanceof Request)) {
            return new Request();
        };
        var instance;
        Request = function () {
            return instance;
        };
        Request.prototype = this;
        instance = new Request();
        instance.constructor = Request;
        var httpRequest = new XMLHttpRequest();
        instance.send = function (query, callback, errorHandler) {
            try { netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead"); } catch (e) { };
            httpRequest.abort();
            if ((query.username == undefined) || (query.password == undefined)) {
                httpRequest.open(query.type, query.endPoint.url, true);
            }
            else {
                httpRequest.open(query.type, query.endPoint.url, true, query.username, query.password);
            };
            if (query.headers != null) {
                for (var i = 0; i < query.headers.length; ++i) {
                    httpRequest.setRequestHeader(query.headers[i][0], query.headers[i][1]);
                }
            }
            else {
                httpRequest.setRequestHeader("SOAPAction", '""');
                httpRequest.setRequestHeader("Content-Type", "text/xml");
            };
            httpRequest.onreadystatechange = function () {
                if (httpRequest.readyState == 4) {
                    if (httpRequest.status == 200) {
                        callback(httpRequest.responseText);
                    }
                    else {
                        if (errorHandler) {
                            errorHandler(httpRequest.status, httpRequest.statusText);
                        }
                        else {
                            callback(null);
                        };
                    };
                };
            };
            switch (query.type) {
                case "POST":
                    httpRequest.send(query.envelope);
                    break;
                case "GET":
                    httpRequest.send(null);
                    break;
            }
        };
        instance.cancel = function () {
            httpRequest.abort();
        };
        return instance;
    };
    var httpHandler = Request();
    var queryQueue = [];
    var timeoutID = -1;
    var startedFlag = false;
    var currentQuery = null;
    var start = function () {
        if (queryQueue.length > 0) {
            startedFlag = true;
            currentQuery = queryQueue.shift();
            httpHandler.send(currentQuery,
                function (resp) {
                    if (timeoutID >= 0) {
                        clearTimeout(timeoutID);
                        timeoutID = -1;
                    };
                    currentQuery.callback_success(resp);
                    start();
                },
                function (resp) {
                    if (timeoutID >= 0) {
                        clearTimeout(timeoutID);
                        timeoutID = -1;
                    };
                    currentQuery.callback_failure(resp);
                    start();
                }
            );
        }
        else {
            startedFlag = false;
        };
    };
    instance.add = function (req) {
        if (req instanceof QueryType) {
            queryQueue.push(req);
            if (!startedFlag) {
                start();
            };
            return true;
        }
        else {
            return false;
        };
    };
    instance.stopCurrent = function () {
        httpHandler.cancel();
        timeoutID = -1;
        if ((currentQuery.callback_failure != undefined) && (currentQuery.callback_failure != null)) {
            currentQuery.callback_failure("<comm_error>COMM TIMEOUT</comm_error>");
        };
        start();
    };
    return instance;
};

//to send a request it must be
TaskManager().add(
    new QueryType({
        "endPoint": new EndPoint("http://***.***.***.***/api/deviceapi"),
        "type": "POST",
        "envelope": '<?xml version="1.0" encoding="UTF-8"?>< ... all xml goes here ',
        "callback_success": function (wResponse) { /* here we get a response from the server */ },
        "callback_failure": function (status, statusText) { },
        "timeout": 60
    })
);
public int Post(object message)
    {//parse message ...
        return 1;
    }
var defineClass = function () {
    var inheritance = function inheritance() { };
    return function defineClass(data) {
        var classname = data.name,
            superclass = data.extend || Object,
            constructor = data.construct || function () { },
            methods = data.methods || {},
            statics = data.statics || {},
            borrows,
            provides;
        if (!data.borrows) {
            borrows = [];
        }
        else {
            if (data.borrows instanceof Array) {
                borrows = data.borrows;
            }
            else {
                borrows = [data.borrows];
            };
        };
        if (!data.provides) {
            provides = [];
        }
        else {
            if (data.provides instanceof Array) {
                provides = data.provides;
            }
            else {
                provides = [data.provides];
            };
        };
        inheritance.prototype = superclass.prototype;
        var proto = new inheritance();
        for (var i = 0; i < borrows.length; i++) {
            var c = borrows[i];
            for (var p in c.prototype) {
                if (typeof c.prototype[p] != "function") continue;
                proto[p] = c.prototype[p];
            }
        }
        for (var p in methods) {
            proto[p] = methods[p];
        };
        proto.constructor = constructor;
        constructor.superclass = superclass.prototype;
        if (classname) {
            proto.classname = classname;
        };
        for (var i = 0; i < provides.length; i++) {
            var c = provides[i];
            for (var p in c.prototype) {
                if (typeof c.prototype[p] != "function") {
                    continue;
                };
                if (p == "constructor" || p == "superclass") {
                    continue;
                };
                if (p in proto && typeof proto[p] == "function" && proto[p].length == c.prototype[p].length) {
                    continue;
                };
                throw new Error("Class " + classname + " are not provided method " + c.classname + "." + p);
            };
        };
        constructor.prototype = proto;
        for (var p in statics) {
            constructor[p] = statics[p];
        };
        return constructor;
    }
}();
var EndPoint = (function () {
    return defineClass({
        name: "EndPoint",
        construct: function (urlObj) {
            var myUrl,
                myScheme,
                myDefaultScheme = "http",
                myLogin,
                myPassword,
                myHost,
                myDefaultHost = "localhost",
                myIP,
                myDefaultIP = "127.0.0.1",
                myPort,
                myDefaultPort = "80",
                myPath,
                myDefaultPath = "/",
                myOptions,
                myAnchor;
            var self = this;
            var parse_url = /^(?:([A-Za-z]+):)?(\/{0,3})(?:([A-Za-z]+):([A-Za-z]+)@)?([0-9.\-A-Za-z]+)(?::(\d+))?(?:\/([^?#]*))?(?:\?([^#]*))?(?:#(.*))?$/;
            var result = parse_url.exec(urlObj);
            var names = ['url', 'scheme', 'slash', 'login', 'password', 'host', 'port', 'path', 'query', 'hash'];
            var i;
            for (i = 0; i < names.length; i += 1) {
                switch (names[i]) {
                    case 'url':
                        myUrl = result[i];
                        break;
                    case 'scheme':
                        myScheme = result[i];
                        break;
                    case 'slash':
                        break;
                    case 'login':
                        myLogin = result[i];
                        break;
                    case 'password':
                        myPassword = result[i];
                        break;
                    case 'host':
                        myHost = result[i];
                        break;
                    case 'port':
                        myPort = result[i];
                        break;
                    case 'path':
                        myPath = result[i];
                        break;
                    case 'query':
                        myOptions = result[i];
                        break;
                    case 'hash':
                        myAnchor = result[i];
                        break;
                }
            }
            this.scheme = myScheme;
            this.login = myLogin;
            this.password = myPassword;
            this.host = myHost;
            this.ip = myIP;
            this.port = myPort;
            this.path = myPath;
            this.options = myOptions;
            this.anchor = myAnchor;
            this.url = myUrl;
        },
        methods: {
            ValidateIP: function (ip) {
                return /^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/.test(ip);
            },
            ValidateUrl: function (url) {
                return /^(http|ftp|https):\/\/[\w-]+(\.[\w-]+)+([\w.,@?^=%&amp;:\/~+#-]*[\w@?^=%&amp;\/~+#-])?/.test(url);
            },
            ValidateShortUrl: function (url) {
                if (/^\/\/[\w-]+(\.[\w-]+)+([\w.,@?^=%&amp;:\/~+#-]*[\w@?^=%&amp;\/~+#-])?/.test(url)) {
                    return true;
                };
                if (/^[\w-]+(\.[\w-]+)+([\w.,@?^=%&amp;:\/~+#-]*[\w@?^=%&amp;\/~+#-])?/.test(url)) {
                    return true;
                }
                return false;
            }
        }
    })
}());
var QueryType = function QueryType() {
    if (!(this instanceof QueryType)) {
        return new QueryType(arguments[0]);
    };
    this.endPoint = arguments[0]["endPoint"];
    this.envelope = arguments[0]["envelope"];
    this.type = arguments[0]["type"];
    this.headers = arguments[0]["headers"];
    this.callback_success = arguments[0]["callback_success"];
    this.callback_failure = arguments[0]["callback_failure"];
    this.timeout = arguments[0]["timeout"];
    this.username = arguments[0]["username"];
    this.password = arguments[0]["password"];
};
var TaskManager = function () {
    if (!(this instanceof TaskManager)) {
        return new TaskManager();
    };
    var instance;
    TaskManager = function () {
        return instance;
    };
    TaskManager.prototype = this;
    instance = new TaskManager();
    instance.constructor = TaskManager;
    function Request() {
        if (!(this instanceof Request)) {
            return new Request();
        };
        var instance;
        Request = function () {
            return instance;
        };
        Request.prototype = this;
        instance = new Request();
        instance.constructor = Request;
        var httpRequest = new XMLHttpRequest();
        instance.send = function (query, callback, errorHandler) {
            try { netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead"); } catch (e) { };
            httpRequest.abort();
            if ((query.username == undefined) || (query.password == undefined)) {
                httpRequest.open(query.type, query.endPoint.url, true);
            }
            else {
                httpRequest.open(query.type, query.endPoint.url, true, query.username, query.password);
            };
            if (query.headers != null) {
                for (var i = 0; i < query.headers.length; ++i) {
                    httpRequest.setRequestHeader(query.headers[i][0], query.headers[i][1]);
                }
            }
            else {
                httpRequest.setRequestHeader("SOAPAction", '""');
                httpRequest.setRequestHeader("Content-Type", "text/xml");
            };
            httpRequest.onreadystatechange = function () {
                if (httpRequest.readyState == 4) {
                    if (httpRequest.status == 200) {
                        callback(httpRequest.responseText);
                    }
                    else {
                        if (errorHandler) {
                            errorHandler(httpRequest.status, httpRequest.statusText);
                        }
                        else {
                            callback(null);
                        };
                    };
                };
            };
            switch (query.type) {
                case "POST":
                    httpRequest.send(query.envelope);
                    break;
                case "GET":
                    httpRequest.send(null);
                    break;
            }
        };
        instance.cancel = function () {
            httpRequest.abort();
        };
        return instance;
    };
    var httpHandler = Request();
    var queryQueue = [];
    var timeoutID = -1;
    var startedFlag = false;
    var currentQuery = null;
    var start = function () {
        if (queryQueue.length > 0) {
            startedFlag = true;
            currentQuery = queryQueue.shift();
            httpHandler.send(currentQuery,
                function (resp) {
                    if (timeoutID >= 0) {
                        clearTimeout(timeoutID);
                        timeoutID = -1;
                    };
                    currentQuery.callback_success(resp);
                    start();
                },
                function (resp) {
                    if (timeoutID >= 0) {
                        clearTimeout(timeoutID);
                        timeoutID = -1;
                    };
                    currentQuery.callback_failure(resp);
                    start();
                }
            );
        }
        else {
            startedFlag = false;
        };
    };
    instance.add = function (req) {
        if (req instanceof QueryType) {
            queryQueue.push(req);
            if (!startedFlag) {
                start();
            };
            return true;
        }
        else {
            return false;
        };
    };
    instance.stopCurrent = function () {
        httpHandler.cancel();
        timeoutID = -1;
        if ((currentQuery.callback_failure != undefined) && (currentQuery.callback_failure != null)) {
            currentQuery.callback_failure("<comm_error>COMM TIMEOUT</comm_error>");
        };
        start();
    };
    return instance;
};

//to send a request it must be
TaskManager().add(
    new QueryType({
        "endPoint": new EndPoint("http://***.***.***.***/api/deviceapi"),
        "type": "POST",
        "envelope": '<?xml version="1.0" encoding="UTF-8"?>< ... all xml goes here ',
        "callback_success": function (wResponse) { /* here we get a response from the server */ },
        "callback_failure": function (status, statusText) { },
        "timeout": 60
    })
);