首页 \ 问答 \ 如何从Reactjs调用Web服务(How to call the webservice from Reactjs)

如何从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
更新时间:2022-07-27 09:07

最满意答案

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 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.

相关问答

更多
  • 现在我有两个问题,第一个问题是每当我尝试使用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 ...
  • 好吧,我的坏,我有一个双功能,做同样的事情,除了第二个没有写到文件...谢谢大家的帮助 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
  • 您只能保存原始数据类型,如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 ...
  • 试试这个在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 ...
  • 我不认为链表是最好的方法。 不要误解我的意思,但链接列表用于在随机位置添加和删除,最常见的是在开头或结尾。 问题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 ...

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。