问题
In postprocesor script I need call API. I am using this code:
var xmlHttp = new XMLHttpRequest();
xmlHttp.open( "GET", "http://www.example.com/something", false );
xmlHttp.send( null );
I got this error:
ReferenceError: "XMLHttpRequest" is not defined. at 8bb4dae5-0615-4f0c-8e60-26c8614a6bcc_Postprocessor:65 (doScript) at 8bb4dae5-0615-4f0c-8e60-26c8614a6bcc_Postprocessor:71 at com.mirth.connect.server.util.javascript.JavaScriptUtil.executeScript(JavaScriptUtil.java:547) at com.mirth.connect.server.util.javascript.JavaScriptUtil.executePostprocessorScripts(JavaScriptUtil.java:275) at com.mirth.connect.server.transformers.JavaScriptPostprocessor$JavaScriptPostProcessorTask.doCall(JavaScriptPostprocessor.java:104) at com.mirth.connect.server.util.javascript.JavaScriptTask.call(JavaScriptTask.java:113) at java.util.concurrent.FutureTask.run(Unknown Source) at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) at java.lang.Thread.run(Unknown Source)
How can I import XMLHttpRequest and using it in postprocesor javascript?
回答1:
XMLHttpRequest is something specific to web browsers. Mirth Connect uses a JavaScript engine called Mozilla Rhino, which is not a web browser oriented engine (because MC isn't a web browser obviously).
Rhino does however seamlessly integrate with the underlying JVM. Basically anything you can do in Java, you can also do within JavaScript. Use URLConnection instead for example:
var url = new java.net.URL('http://www.google.com');
var conn = url.openConnection();
var is = conn.getInputStream();
try {
var result = org.apache.commons.io.IOUtils.toString(is, 'UTF-8');
} finally {
is.close();
}
来源:https://stackoverflow.com/questions/45554677/mirth-javascript-import