0

I am trying to get some data and set a page to be the result

function getData() {
    $.ajax({
        url:"http://localhost:8080/",
        type:'GET',
        dataType: 'html',
        success: function(data){
            console.log(data);
            $('.App').html(data);
        }
    });
}

class App extends Component {
  render() {
    getData()
    return (
      <div className="App">This worked</div>
    );
  }
}

export default App;

This is the response being printed out:

<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1,shrink-to-fit=no"><meta name="theme-color" content="#000000"><link rel="manifest" href="/manifest.json"><link rel="shortcut icon" href="/favicon.ico"><title>React App</title><link href="/static/css/main.c17080f1.css" rel="stylesheet"></head><body><div id="root"><div class="App" data-reactroot=""><header class="App-header"><img src="logo.svg" class="App-logo" alt="logo"/><h1 class="App-title">Welcome to React</h1></header><div></div></div></div><script type="text/javascript" src="/static/js/main.d1b4ad06.js"></script></body></html>

When the page loads, it flashes the correct HTML for a second, then I get this error:

VM11047:1 Uncaught SyntaxError: Unexpected token <

What am I doing wrong?

1
  • there's nothing wrong with using jquery with react as long as you stay away from it's dom manipulation methods such as .html() (which makes up most of the library). You should instead be updating data that causes the react component to render said data once available. Commented Nov 27, 2017 at 22:06

1 Answer 1

1

You could use react's dangerouslySetInnerHTML prop, more info here https://reactjs.org/docs/dom-elements.html

or the safer way to handle is to return a json response and create components to display that returned data

there is also an interesting article on why not to mix jquery dom manipulation and react. http://tech.oyster.com/using-react-and-jquery-together/

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.