如何修复“错误:XMLHttpRequest失败";在具有解析服务器后端的React Native上 [英] How to fix "Error: XMLHttpRequest failed" on React Native with Parse-Server backend

查看:125
本文介绍了如何修复“错误:XMLHttpRequest失败";在具有解析服务器后端的React Native上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

今天,我有一个计划将iOS应用程序(Swift)和Android(Java + Kotlin)转换为React Native. 此应用根据用户位置在(Google)地图上将医院显示为标记. 我的后端是Parser-Server(现在是ParsePlatform)

Today I have a plan to convert an iOS app (Swift) and Android (Java+Kotlin) to React Native. This app show hospital as Marker on (Google)Maps based on user location. My backend is Parser-Server (now is ParsePlatform)

下面的函数从Parse后端查询数据(类):

The function bellow query the data (class) from Parse backend:

async getHospitalNearbyUser() {
        const { userLocation, region } = this.state
        if ((userLocation.lat !== null) && (userLocation.lng !== null)) {
            const lng = userLocation.lng
            const lat = userLocation.lat
            const radius = 1.0

            let lng_min = lng - radius / Math.abs(Math.cos(this.deg2rad(lat)) * 69)
            let lng_max = lng + radius / Math.abs(Math.cos(this.deg2rad(lat)) * 69)

            let lat_min = lat - (radius / 69)
            let lat_max = lat + (radius / 69)

            // query
            const atm = Parse.Object.extend('Hospital')
            let query = new Parse.Query(atm)
            query.greaterThan("lat", lat_min)
            query.lessThan("lat", lat_max)
            query.greaterThan("lng", lng_min)
            query.lessThan("lng", lng_max)

            query.find()
                .then(function (results) {
                    console.debug(results.length)
                })
                .catch(function (error) {
                    // There was an error.
                    console.error(error)
                })
        } else {
            console.debug('no location')
            console.debug(region)
        }
    }

但是出现的错误是:

Error: XMLHttpRequest failed: {"line":169557, "column:32", "sourceURL":"http://localhost:8081/index.bundle?platform=ios&dev=true&minifiy=false"

我试图在Google上搜索并在github问题上解析Parse SDK-JS,但没有结果.

I tried to seach on Google and Parse SDK-JS on github issues but no results.

希望您能帮上忙.

非常感谢!

//更新: 我正在使用Back4App托管的Parse-Server.

// Update: I am using Parse-Server that is hosted at Back4App.

推荐答案

这很可能是AsyncStoragehttps的问题.

  1. 确保您已正确导入并设置AsyncStorage.

import AsyncStorage from '@react-native-community/async-storage'

...

Parse.setAsyncStorage(AsyncStorage)
Parse.serverURL = 'https://parseapi.back4app.com/'

...

不要不要react-native导入AsyncStorage,就像在另一个答复中所建议的那样.不建议使用此方法,而不是适当的解决方案.

Do NOT import AsyncStorage from react-native like suggested in another reply. This is deprecated and not the proper solution.

  1. 确保您使用的服务器URL是https://...而不是http://....
  1. Make sure you are using https://... and not http://... for your server url.

希望这会有所帮助.

这篇关于如何修复“错误:XMLHttpRequest失败";在具有解析服务器后端的React Native上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆