如何在React-Native中使用带有return的全局函数?

时间:2019-07-25 04:43:01

标签: javascript reactjs react-native react-native-android

我想创建一个全局帮助程序文件,因为大多数页面将调用相同的函数但参数不同。我尝试了几种方法,但都没有返回值,就像我将脚本放在同一屏幕上一样。

我正在测试以获取SOAP WSDL的响应。

URL: 'http://mytesting.justdummy.com/android.asmx' 
Content-Type: text/xml 
SOAPAction: http://tempuri.org/GetServerTime 

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <GetServerTime xmlns="http://tempuri.org/" />
  </soap:Body>
</soap:Envelope>

我已经使用Postman进行了测试,并且获得了服务器时间响应。

以下是我目前尝试的内容: 方法1:

const soapAPI= {
    fetchSOAPRequest: function (soapFunction, soapBody) {
        var xmlhttp = new XMLHttpRequest();
        xmlhttp.open('POST', global.valServerURL, true);
        // build SOAP request
        var soapRequest =
            '<?xml version="1.0" encoding="utf-8"?>' +
            '<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">' +
            '<soap:Body>' +
            `<${soapFunction} xmlns="http://tempuri.org/">` +
            soapBody +
            `</${soapFunction}>` +
            '</soap:Body>' +
            '</soap:Envelope>';

        xmlhttp.onreadystatechange = function () {
            if (xmlhttp.readyState == 4) {
                if (xmlhttp.status == 200) {
                    return xmlhttp.responseText;
                }
            }
        }
        // Send the POST request
        xmlhttp.setRequestHeader('Content-Type', 'text/xml');
        xmlhttp.setRequestHeader('SOAPAction', `http://tempuri.org/${soapFunction}`);
        xmlhttp.send(soapRequest);
    }
}
export default soapAPI;

方法2:

export const fetchSOAPRequest = async (soapFunction, soapBody) => {
    var xmlhttp = new XMLHttpRequest();
    xmlhttp.open('POST', global.valServerURL, true);
    // build SOAP request
    var soapRequest =
        '<?xml version="1.0" encoding="utf-8"?>' +
        '<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">' +
        '<soap:Body>' +
        `<${soapFunction} xmlns="http://tempuri.org/">` +
        soapBody +
        `</${soapFunction}>` +
        '</soap:Body>' +
        '</soap:Envelope>';

    console.warn(soapRequest);
    xmlhttp.onreadystatechange = function () {
        if (xmlhttp.readyState == 4) {
            if (xmlhttp.status == 200) {
                return xmlhttp.responseText;
            }
        }
    }
    // Send the POST request
    xmlhttp.setRequestHeader('Content-Type', 'text/xml');
    xmlhttp.setRequestHeader('SOAPAction', `http://tempuri.org/${soapFunction}`);
    xmlhttp.send(soapRequest);
};

here中的方法3:

function fetchSOAPRequest(soapFunction, soapBody) {
    var xmlhttp = new XMLHttpRequest();
    xmlhttp.open('POST', global.valServerURL, true);
    // build SOAP request
    var soapRequest =
        '<?xml version="1.0" encoding="utf-8"?>' +
        '<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">' +
        '<soap:Body>' +
        `<${soapFunction} xmlns="http://tempuri.org/">` +
        soapBody +
        `</${soapFunction}>` +
        '</soap:Body>' +
        '</soap:Envelope>';

    console.warn(soapRequest);
    xmlhttp.onreadystatechange = function () {
        if (xmlhttp.readyState == 4) {
            if (xmlhttp.status == 200) {
                return xmlhttp.responseText;
            }
        }
    }
    // Send the POST request
    xmlhttp.setRequestHeader('Content-Type', 'text/xml');
    xmlhttp.setRequestHeader('SOAPAction', `http://tempuri.org/${soapFunction}`);
    xmlhttp.send(soapRequest);
}
function fetchAnotherAPI(date) {
    // your logic
 }
export {
    fetchSOAPRequest,
 };

方法1和2位于相同的“ ../components/services/soapAPI”文件中

在应该返回的主类上。

import { fetchSOAPRequest, soapAPI} from '../components/services/soapAPI';
onTestingRetrieve = async () => {
    try {
        // call from export constant
        var myfirst = await fetchSOAPRequest('GetServerTime', '');
        // call from object and function
        var mysecond = await soapAPI.fetchSOAPRequest('GetServerTime', '');
        // call from function within same class file
        var mythird = await this.fetchSOAP('GetServerTime', '');
        // alert to show response for confirmation only
        Alert.alert('Response', JSON.stringify(resp));
    } catch (e) {
        Alert.alert('Error', 'SOAP request failed!\n' + e);
    }
}
//On render
<TouchableOpacity style={styles.buttonContainer} onPress={this.onTestingRetrieve}>
    <Text style={styles.buttonText}>Get server time</Text>
</TouchableOpacity>

但是,如果我将XMLHttpRequest放在onTestingRetrieve中,它将显示出responseText,如下所示。

onTestingRetrieve = async () => {
    try {
        const soapFunction = 'GetServerTime';
        const soapBody = '';
        var xmlhttp = new XMLHttpRequest();
        xmlhttp.open('POST', global.valServerURL, true);
        // build SOAP request
        var soapRequest =
            '<?xml version="1.0" encoding="utf-8"?>' +
            '<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">' +
            '<soap:Body>' +
            `<${soapFunction} xmlns="http://tempuri.org/">` +
            soapBody +
            `</${soapFunction}>` +
            '</soap:Body>' +
            '</soap:Envelope>';
        xmlhttp.onreadystatechange = function () {
            if (xmlhttp.readyState == 4) {
                if (xmlhttp.status == 200) {
                    Alert.alert('Response', xmlhttp.responseText);
                }
            }
        }
        // Send the POST request
        xmlhttp.setRequestHeader('Content-Type', 'text/xml');
        xmlhttp.setRequestHeader('SOAPAction', `http://tempuri.org/${soapFunction}`);
        xmlhttp.send(soapRequest);

    } catch (e) {
        Alert.alert('Error', 'SOAP request failed!\n' + e);
    }
}

我已经搜索了教程,示例和SO问题,但是我仍然无法获得服务器时间。任何人都可以浏览脚本并指出我缺少的部分吗?

0 个答案:

没有答案