Javascript &引用;类型错误:“u jquery2.default.ajax不是函数”;在react本地项目中
我正在尝试使用jQuery从内部网站提取xml数据。我有一个基本的世博会项目设置,如下所示:Javascript &引用;类型错误:“u jquery2.default.ajax不是函数”;在react本地项目中,javascript,reactjs,react-native,Javascript,Reactjs,React Native,我正在尝试使用jQuery从内部网站提取xml数据。我有一个基本的世博会项目设置,如下所示: import React from 'react'; import { StyleSheet, Text, View } from 'react-native'; import $ from 'jquery'; import {XML_DATA_URL} from './src/api/constants'; export default class App extends React.Compon
import React from 'react';
import { StyleSheet, Text, View } from 'react-native';
import $ from 'jquery';
import {XML_DATA_URL} from './src/api/constants';
export default class App extends React.Component {
fetchData = () => {
return $.ajax({
url: XML_DATA_URL,
dataType: 'xml',
});
}
render() {
let response = 'test';
response = this.fetchQviewData();
console.log(response);
return (
<View >
<Text>Open up App.js to start working on your app!</Text>
</View>
);
}
}
我的环境是visual studio代码,当我将鼠标悬停在函数上时,可以看到原型:
我错过什么了吗?谢谢。我想我在中找到了答案,问题似乎是因为react native使用虚拟DOM,并且没有实际的html或css,那么jQuery将无法工作,因为它需要访问DOM。不是100%确定发生了什么(我在早期react中使用了jQuery而不是包),但我建议使用
fetch()
API。我希望使用fetch()API,但不幸的是,我正在尝试访问的网站上有一些http内容,因此fetch()调用引发一个错误:“位于[代码行号]的页面是通过https加载的,但请求的资源不安全”fetch仅支持https请求。创建错误的不是fetch命令。检查服务器是否配置了ssl,如果是,则检查url是否为“http://”,然后将其更改为“https://”。如果我确定,则问题在于您正在返回ajax调用,请尝试返回在成功回调中收到的响应。
{
"name": "empty-project-template",
"main": "node_modules/expo/AppEntry.js",
"private": true,
"scripts": {
"start": "exp start --lan",
"android": "exp start --lan --android",
"ios": "exp start --ios",
"eject": "exp eject"
},
"dependencies": {
"expo": "^29.0.0",
"jquery": "3.1.1",
"react": "16.3.1",
"react-native": "https://github.com/expo/react-native/archive/sdk-
29.0.0.tar.gz",
"yarn": "^1.9.4"
}
}