2016-09-29 51 views
0

我有以下代碼:陣營componentDidMount沒有觸發jQuery的AJAX調用

import React from 'react'; 
import {render} from 'react-dom'; 

class App extends React.Component { 
    constructor(props) { 
     super(props); 
     this.state = { 
      data: '' 
     }; 
    } 
    componentDidMount() { 
     $.get(this.props.source, function(result) { 
     this.setState({data: result}); 
     }.bind(this)); 
    } 
    render() { 
     return (
      <div> 
       <p>{this.state.data}</p> 
      </div> 
     ) 
    } 
} 

render(<App source='https://s3-eu-west-1.amazonaws.com/streetlife-coding-challenge/newsfeed.json'/>, document.getElementById('app')); 

的package.json:

{ 
    "name": "streetlife", 
    "version": "1.0.0", 
    "description": "technical test for streetlife", 
    "main": "index.js", 
    "scripts": { 
    "test": "echo \"Error: no test specified\" && exit 1" 
    }, 
    "author": "", 
    "license": "ISC", 
    "dependencies": { 
    "babel-core": "^6.16.0", 
    "babel-loader": "^6.2.5", 
    "babel-preset-es2015": "^6.16.0", 
    "babel-preset-react": "^6.16.0", 
    "react": "^15.3.2", 
    "react-dom": "^15.3.2", 
    "webpack": "^1.13.2" 
    } 
} 

我期望在我的 「P」 標籤提供的JSON數據但屏幕是空的,我也沒有得到錯誤。

回答

1

嗯,我把你的代碼粘貼到JSBin中,看看發生了什麼,但顯然它工作。這裏的code working。也許這與你的環境有關嗎?

+0

我在環境中沒有得到任何錯誤... – Alex

+0

您認爲什麼樣的環境問題? – Alex

+0

解決了,我在腳本標記中加載了jQuery。我刪除了這個,並且在上面的jsx文件中導入了它。謝謝 – Alex