简体   繁体   English

使用mobileFirst javascript适配器读取本地服务器.json文件

[英]Reading local server .json file with mobileFirst javascript adapter

Is there any way that I can read a .json file (located in server) from a javascript http adapter? 有什么办法可以从javascript http适配器读取.json文件(位于服务器中)? I tried a lot of methods described in the internet but they don't seem to work because they are made for browser javascript (I get errors like XMLHttpRequest is not defined or activeObject is not defined). 我尝试了互联网上描述的许多方法,但它们似乎不起作用,因为它们是为浏览器javascript编写的(我收到未定义XMLHttpRequest或activeObject的错误)。

for example, I used this but it doesn't work: 例如,我使用了它,但是它不起作用:

function readTextFile(file)
{
    var rawFile = new XMLHttpRequest();
    rawFile.open("GET", file, false);
    rawFile.onreadystatechange = function ()
    {
        if(rawFile.readyState === 4)
        {
            if(rawFile.status === 200 || rawFile.status == 0)
            {
                var allText = rawFile.responseText;
                return allText;
            }
        }
    }
    rawFile.send(null);
}  

Is there any way that I could do this without using java? 有没有不使用Java就能做到这一点的方法?

You can read a file with Javascript as shown below. 您可以使用Javascript读取文件,如下所示。

function readFile(filename) {
    var content = "";

    var fileReader = new java.io.FileReader(filename);

    var bufferedReader = new java.io.BufferedReader(fileReader);

    var line;

    while((line = bufferedReader.readLine()) != null) {
        content += line;
    }   

    bufferedReader.close();

    return content;
}

function test() {
    var file = 'yourfilename.json';
    var fileContents;
    try {
         fileContents = JSON.parse(readFile(file));     
    }  catch(ex) {
        // handle error                
    }

    return  {
        fileContents: fileContents
    };
}

For those interested in using Java. 对于那些对使用Java感兴趣的人。

One thing you can do is create a Javascript adapter which will use Java code. 您可以做的一件事是创建一个将使用Java代码的Javascript适配器。 It is pretty simple to set up. 设置非常简单。

First create a Javascript adapter. 首先创建一个Javascript适配器。

Then create a Java class under the server/lib folder. 然后在server/lib文件夹下创建一个Java类。 I created the class ReadJSON.java under the package com.sample.customcode . 我创建的类ReadJSON.java包下com.sample.customcode

Inside the ReadJSON.java ReadJSON.java

public class ReadJSON {
    public static String readJSON() throws IOException {
       //Open File
        File file = new File("file.txt");
        BufferedReader reader = null;
        try {
           //Create the file reader
            reader = new BufferedReader(new FileReader(file));
            String text = null;

            //read file
            while ((text = reader.readLine()) != null) {}
        } finally {
             try {
                 //Close the stream
                 reader.close();
             } 
        }
        return "the text from file";
    }
}

Inside your javascript adapter you can use Java methods like below: 在您的javascript适配器内,您可以使用如下所示的Java方法:

function readJOSN() {
    var JSONfromServer = com.sample.customcode.ReadJSON.readJSON();
    return {
        result: JSONfromServer
    };

}

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM