2016-04-03 24 views
0

Benim api, sorguyu http://localhost:8000/api/v1/rental/?place__startswith=kathmandu olarak alırsa, reajs'de nasıl genel arama yapabilirim? Denediğim şey, varsayılan parametreyi search (query = kathmandu) olarak geçtim, böylece kathmandu adlı yerin sonucu varsayılan olarak listelenecek ve kullanıcı arama yapmak istedikleri adı yerleştirdiğinde kathmndu yerine bu yerleri göstermelidir. Ama Uncaught ReferenceError: kathmandu tanımlı değil diyerek bir hata alıyorum. Bu sorunu nasıl çözebilirim?İşlevde bir parametre olarak hiçbir tırnak içermeyen dize nasıl iletilir?

componentWillMount(){ 
     this.search(); 
     } 

search(query=kathmandu){ 
    let url = 'http://localhost:8000/api/v1/rental/?place__startswith=query'; 
    Request.get(url).then((response) => { 
     console.log('response',response.body.objects); 
     this.setState({ 
      place:response.body.objects 
     }); 
    }); 
    } 

    searchUpdated(term){ 
    console.log('term is',term); 
    this.search(term); 
    } 

    render() { 
     var margin = { marginTop : '13em' }; 
     let location = _.map(this.state.place, (place) => { 
      return(
        <div className="searchResult"> 
         <li>{place.place}</li> 
         <li>{place.city}</li> 
        </div> 
       ) 
     }); 
     return(
      <div className = "container"> 
       <div className="content text-align-center"> 
        <div className="row text-xs-center"> 
         <div className="middle-text" style={margin}> 
          <h1 className="welcome"><span>Welcome </span></h1> 
          <button ref="test" className="btn how-it-works" onClick={this.handleClick}>Search Space</button> 
         </div> 
        </div> 
       </div> 
       <div id="mySearch" className="overlay" onKeyDown={this.handleKeyDown}> 
        <button className="btn closebtn" onClick={this.handleClick}>x</button> 
        <div className="overlay-content"> 
         <SearchInput ref="searchInput" className="search-input" onChange={this.searchUpdated} /> 
         <ul>{location}</ul> 
        </div> 
      </div> 
      </div> 
      ); 
    } 
} 
+1

Arama (query = kathmandu) {'be' arama (query = "kathmandu") {'? –

+0

Benim api olarak çalışmıyorum, http: // localhost: 8000/api/v1/rental /? Place__startswith = kathmandu biçimindeki url sorgusunu http: // localhost: 8000/api/v1/rental yerine almaz. /? place__startswith = 'kathmandu' – pri

+0

Neden oy kullanıyorum? – pri

cevap

2

Aradığınız şeyin encodeURIComponent olduğunu düşünüyorum.

search(query='kathmandu'){ 

Ve:

let url = 'http://localhost:8000/api/v1/rental/?place__startswith=' + encodeURIComponent(query);

sorgu dizesi aslında harfi içeren tek vermez NB olarak, bu örneğin encodeURIComponent gerekmez, ancak diğer durumlarda bunu gerekebilir.

+0

Bunu yaparken boş yanıt alıyorum. – pri

+0

'encodeURIComponent ('" kathmandu "')'? – Ilya

+0

Geçiş parametresi çalışmıyor, hatta encodeURIComponent ('"kathmandu"'). ama bu işe yarıyor. componentWillMount() { \t this.search(); } search() { \t let url = 'http: // localhost: 8000/api/v1/rental /? Place__startswith = kathmandu'; \t Request.get (URL) .ve ((yanıt) => { \t \t console.log ('tepki', response.body.objects); \t \t this.setState ({ \t \t \t Yer: tepki \t \t}); \t}); } searchUpdated (term) { \t console.log ('term is', term); \t this.search(); } – pri