如何从Reactjs调用Web服务(How to call the webservice from Reactjs)
我正在研究我正在使用带有webpack的Reactjs的示例。 如何从reactjs组件调用web服务并将结果传递给html文件
我试过superagent,http-browserify等所有这些都给了我同样的错误:
未捕获的TypeError:无法读取未定义的属性'get'。
下面是我调用API的视图组件
import React from 'react' import FindpersonStore from'../stores/FindPersonStore'; import PersonAPI from'../utils/PersonAPI'; var FindPerson = React.createClass({ handleGoClick() { PersonAPI.getPerson(this.refs.personIdInput.getDOMNode().value); }, render(){ return( <div> <p>person Id <input type="number" value={this.props.personId} ref="personIdInput" /></p> <input type="Submit" onClick={this.handleGoClick}/> </div> ); } }); export default FindPerson
API就是
import FindPersonAction from'../actions/FindPersonAction'; import {request} from 'superagent'; module.exports = { // Load mock product data from localStorage into ProductStore via Action getPerson: function(personId) { var url = "http://localhost:8080/SampleWebService/service/find?id="+personId request .get(url)//This is giving me error .end(function(res){ if (res.status === 404) { reject(); } else { var data = JSON.parse(res); FindPersonAction.findPerson(data); } }) } };
I am working on sample for which I am using Reactjs with webpack. How to call a webservice from reactjs components and pass the result to html file
I tried superagent, http-browserify etc All of them giving me same error :
Uncaught TypeError: Cannot read property 'get' of undefined.
Below is the view component from where I am calling the API
import React from 'react' import FindpersonStore from'../stores/FindPersonStore'; import PersonAPI from'../utils/PersonAPI'; var FindPerson = React.createClass({ handleGoClick() { PersonAPI.getPerson(this.refs.personIdInput.getDOMNode().value); }, render(){ return( <div> <p>person Id <input type="number" value={this.props.personId} ref="personIdInput" /></p> <input type="Submit" onClick={this.handleGoClick}/> </div> ); } }); export default FindPerson
And The API is
import FindPersonAction from'../actions/FindPersonAction'; import {request} from 'superagent'; module.exports = { // Load mock product data from localStorage into ProductStore via Action getPerson: function(personId) { var url = "http://localhost:8080/SampleWebService/service/find?id="+personId request .get(url)//This is giving me error .end(function(res){ if (res.status === 404) { reject(); } else { var data = JSON.parse(res); FindPersonAction.findPerson(data); } }) } };
原文:https://stackoverflow.com/questions/33006976
最满意答案
在
loadArray()
方法中,size
默认为0.在第一次运行时,不会有任何已保存的首选项,因此size
为0,这就是您用来标注数组的方法。由于您在
editHighscores()
方法中检查空值,因此无需跟踪大小。 你已经确定了数组应该有多大:5。In the
loadArray()
method,size
defaults to 0. On your first run, there won't be any saved preference, so thesize
is 0, and this is what you're using to dimension the array.Since you're checking for null values in the
editHighscores()
method, you don't need to track the size. You've already decided how big the array should be: 5.
相关问答
更多-
下列中不属于面向对象的编程语言的是?[2022-05-30]
a -
共享偏好VS上下文?(Shared Preference VS Context?)[2024-04-14]
现在我有两个问题,第一个问题是每当我尝试使用Globals类时我的应用程序都会崩溃,这里是代码:我是否错误地使用了上下文? 你应该使用getApplication()方法,或者让你的应用程序类单例,所以你可以调用Globals.getInstance().getMyVariable()等。 我的最后一个问题是:我在这里已经阅读了一些答案,如果我的应用程序成为后台进程,那么上下文中的所有数据都将设置为null,我将丢失我的上下文。 有没有办法克服这一点? 或者我应该切换到SharedPreferences? ... -
不可能。 您只能在SharedPrefences SharePreferences.Editor中存储,简单的值 你需要保存什么特别的课程? Not possible. You can only store, simple values in SharedPrefences SharePreferences.Editor What particularly about the class do you need to save?
-
SELECT name, score FROM SCORES JOIN (SELECT distinct score score3 FROM scores ORDER BY score DESC LIMIT 2, 1) x ON score >= score3 ORDER by score DESC 小提琴 SELECT name, score FROM SCORES JOIN (SELECT distinct score score3 FROM score ...
-
共享偏好不相似(shared preference not similar)[2023-07-07]
好吧,我的坏,我有一个双功能,做同样的事情,除了第二个没有写到文件...谢谢大家的帮助 Well my bad, I had a double function that was doing the same thing except the second one was not writing it to the file... Thanks everybody for your help -
是否可以在共享首选项中存储Class的静态对象?(Is it possible to stored Class's static object in Shared Preference?)[2022-05-28]
您只能保存原始数据类型,如float,int,string,字符串集或布尔值。 你可以在字节流中序列化你的对象,然后从字节流中重新创建它们,你可以看看这个答案也是这个答案 You can only save primitive data types like float, int, string, set of strings or booleans. You can serialize your objects in byte stream and then reacreate them from the ... -
共享偏好问题(Problem with shared preference)[2022-01-05]
试试这个在sharePreferences中的商店价值.. SharedPreferences prefs = getSharedPreferences("Share", Context.MODE_PRIVATE ); Editor editor = prefs.edit(); editor.putInt("Value", 1 ); editor.commit(); 获得价值 prefs.getInt("Value",0); try this for store value in sharePrefer ... -
将前10个分数写入文件(Writing top 10 scores to file)[2022-12-07]
我不认为链表是最好的方法。 不要误解我的意思,但链接列表用于在随机位置添加和删除,最常见的是在开头或结尾。 问题whit链表是它与普通数组相同的低效率,因为它必须检查每个元素,直到找到它为止。 我认为使用数组将使您的程序具有相同的效率和更简单的代码。 比较两种数据结构(链表和数组): 查找元素在两种情况下均与长度成比例。 两端的插入元素在两个结构中都是恒定的。 链接列表可以有效地在任何位置添加元素,但必须找到位置,因此这会补偿数组所具有的问题,您必须将右侧元素向右移动一个位置。 我觉得有点像 typed ... -
在loadArray()方法中, size默认为0.在第一次运行时,不会有任何已保存的首选项,因此size为0,这就是您用来标注数组的方法。 由于您在editHighscores()方法中检查空值,因此无需跟踪大小。 你已经确定了数组应该有多大:5。 In the loadArray() method, size defaults to 0. On your first run, there won't be any saved preference, so the size is 0, and this ...
-
文档中提到的大小是“值可以具有的最大字符数”。 不是您可以存储的元素数量。 虽然您可以存储的项目数量没有固有的限制 - 随着项目数量的增加,检索和存储变得非常昂贵。 SharedPreferences用于整个应用程序可能需要的全局常量,而不是数据库替换。 您无法对其执行数据操作和其他SQLite操作。 所以当数据很重要时,总是去数据库。 The size mentioned in the docs is the "maximum no of characters a value can have". Not ...