Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [PHP][JavaScript]UI-autocomplete nie działa na serwerze
nansss93
post
Post #1





Grupa: Zarejestrowani
Postów: 45
Pomógł: 1
Dołączył: 27.10.2015

Ostrzeżenie: (0%)
-----


Witam,
Korzystając z ui-autocomplete napisałem sobie wyszukiwarkę z podpowiadaniem, problem w tym, że na localhost działa, ale na serwerze produkcyjnym już nie(zero błędów w konsoli, request do php się wykonuje jednak response jest pusty, natomiast gdy tam print_r przed return w php zwraca poprawną tablicę wyników).

  1. var cache = {};
  2. var search = $("#search");
  3. search.autocomplete({
  4. minLength: 1,
  5. autoFocus: true,
  6. source: function(request,response){
  7. var term = request.term;
  8. if(term in cache){
  9. response(cache[term]);
  10. return;
  11. }
  12. $.getJSON("/pl/search/search",request,function(data){
  13. cache[term] = data;
  14. response(data);
  15. });
  16. },
  17. select: function( event, ui ) {
  18. if(ui.item.value!='Brak rezultatów' && ui.item.value!='Keine Ergebnisse gefunden' && ui.item.value!='No result' && ui.item.value!='Найденные'){
  19. $('#search').val(ui.item.value);
  20. $("input.buttonS").trigger("click");
  21. }
  22. }
  23. });


  1. public function searchAction(){
  2. $this->view->disable();
  3. $lang = parent::getTranslation();
  4. $data = $this->request->get();
  5. $req = $data['term'];
  6. $item = new \Models\Item\Item();
  7. $items = $item->getItemByNameAndLang($req, $_SESSION['lang']);
  8. $ret = array();
  9. foreach($items as $it){
  10. $ret[] = $it->getName($_SESSION['lang']);
  11. }
  12. if(empty($ret)){
  13. $ret[] = $lang->_("nosearch");
  14. }
  15. return json_encode($ret);
  16. }


Co robię nie tak?
Go to the top of the page
+Quote Post
kapslokk
post
Post #2





Grupa: Zarejestrowani
Postów: 965
Pomógł: 285
Dołączył: 19.06.2015
Skąd: Warszawa

Ostrzeżenie: (0%)
-----


Jaka masz wersje php na serwerze produkcyjnym? Wyswietlanie/logowanie bledow na pewno wlaczone?
Go to the top of the page
+Quote Post
nospor
post
Post #3





Grupa: Moderatorzy
Postów: 36 557
Pomógł: 6315
Dołączył: 27.12.2004




Pewnie masz problem z utf8 i json_encode sie normalnie w swiecie wyklada. Albo w ogole nie masz json_encode funkcji biggrin.gif

var_dump(json_encode($ret));


--------------------

"Myśl, myśl, myśl..." - Kubuś Puchatek || "Manual, manual, manual..." - Kubuś Programista
"Szukaj, szukaj, szukaj..." - Kubuś Odkrywca || "Debuguj, debuguj, debuguj..." - Kubuś Developer

Go to the top of the page
+Quote Post
nansss93
post
Post #4





Grupa: Zarejestrowani
Postów: 45
Pomógł: 1
Dołączył: 27.10.2015

Ostrzeżenie: (0%)
-----


  1. var_dump(json_encode($ret));


zwraca

string(52) "["Produkt","Produkt","Produkt","Produkt","Produkt2"]"

Wersja PHP na serwerze:

PHP Version 5.6.26-1~dotdeb+7.1
Go to the top of the page
+Quote Post
nospor
post
Post #5





Grupa: Moderatorzy
Postów: 36 557
Pomógł: 6315
Dołączył: 27.12.2004




No to problem masz poza funkcja searchAction(). Szukaj dalej


--------------------

"Myśl, myśl, myśl..." - Kubuś Puchatek || "Manual, manual, manual..." - Kubuś Programista
"Szukaj, szukaj, szukaj..." - Kubuś Odkrywca || "Debuguj, debuguj, debuguj..." - Kubuś Developer

Go to the top of the page
+Quote Post
nansss93
post
Post #6





Grupa: Zarejestrowani
Postów: 45
Pomógł: 1
Dołączył: 27.10.2015

Ostrzeżenie: (0%)
-----


Przerobiłem trochę kod js:

  1. var cache = {};
  2. var search = $("#search");
  3. search.autocomplete({
  4. minLength: 1,
  5. autoFocus: true,
  6. source: function(request,response){
  7. var term = request.term;
  8. if(term in cache){
  9. response(cache[term]);
  10. return;
  11. }
  12. var test = $.getJSON("/pl/search/search",request,function(data){
  13. cache[term] = data;
  14. console.log('1');
  15. response(data);
  16. })
  17. .done(function() {
  18. console.log( "second success" );
  19. })
  20. .fail(function() {
  21. console.log( "error" );
  22. })
  23. .always(function() {
  24. console.log( "complete" );
  25. });
  26. test.complete(function() {
  27. console.log( "second complete" );
  28. });
  29. },
  30. select: function( event, ui ) {
  31. if(ui.item.value!='Brak rezultatów' && ui.item.value!='Keine Ergebnisse gefunden' && ui.item.value!='No result' && ui.item.value!='questionmark.gifquestionmark.gifquestionmark.gifquestionmark.gif?'){
  32. $('#search').val(ui.item.value);
  33. $("input.buttonS").trigger("click");
  34. }
  35. }


i na serwerze mam w console:

Kod
error
complete
second complete


a na localu:

Kod
1
second success
complete
second complete


poza tym pusto w konsoli

Udało mi się jeszcze uzyskać treść błędu:

Kod
Request Failed: parsererror, SyntaxError: Unexpected end of JSON input


Ok, mam. Zmieniłem:

  1. return json_encode($ret);


na

  1. $result = json_encode($ret);
  2. echo $result;


Ten post edytował nansss93 12.10.2016, 19:01:36
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 19.08.2025 - 13:13