0

Here's two scripts for an e-commerce website, it takes the input from the search bar and creates an httprequest to the AWS api (in order to show the searched item page). To make it easier I'm trying to take just the first input word.

I even tried to use onreadystatechange instead of onload.

In the HTML I included search.js before pageLoading.js so that it's in the right order.

pageLoading.js:

var searchBar = document.getElementById("searchBarH");

searchBar.addEventListener("search", function(Event) {
  searchByInput();
});

search.js:

function searchByInput() {
  function init() {
    var request = new XMLHttpRequest();
    return request;
  }
  function start(nameSearch) {
    xmlHttp.open("GET", nameSearch, true);
    xmlHttp.onreadystatechange = fun();
    xmlHttp.send();
  }
  function fun() {
    alert(xmlHttp.readyState); // it prints 1
    object = JSON.parse(xmlHttp.response); //can't get the response because readyState=1
    window.location.assign('file:///C:/MAMP/htdocs/10shop/product.html?id=' + object.items[3].id);
  }
  var textInput = document.getElementById("searchBarH").value;
  var searchTokens = new Array(" ");
  var object;
  searchTokens = textInput.split(" ");
  const itemApi = '*AWS API URL*';
  var nameSearch = itemApi + "?name=" + searchTokens[0];
  var xmlHttp = init();
  var starttt = start(nameSearch);
}
Dharman
  • 30,962
  • 25
  • 85
  • 135
  • 1
    `xmlHttp.onreadystatechange = fun()` --> `xmlHttp.onreadystatechange = fun` – Ivar Sep 17 '19 at 13:03
  • _This question was caused by a problem that can no longer be reproduced or a simple typographical error. While similar questions may be on-topic here, this one was resolved in a manner unlikely to help future readers_ – mplungjan Sep 17 '19 at 13:04
  • Also this should run on a server and then it would be `location = '/product.html?id=' + object.items[3].id;` – mplungjan Sep 17 '19 at 13:07
  • I tried but it's not solved – Chiara Pellecchia Sep 17 '19 at 13:11
  • @ChiaraPellecchia Two lines after your alert you already redirect to a different location. You never give it a chance to get past ready state 1. – Ivar Sep 17 '19 at 13:19
  • @Ivar where do you think I should put the redirect? I tried at the end of the code, but it doesn't enter in fun() anymore. It's very likely that the problem is in xmlHttp.response, also because if I try to write 'alert(object)' it gives me null. I'm sure that the JSON works perfectly (I tried the url on my browser). – Chiara Pellecchia Sep 17 '19 at 13:29
  • @ChiaraPellecchia Personally I don't really see the benefit of AJAX if you are redirecting it afterwards, but have a look at [this post](https://stackoverflow.com/a/8567149). Put the redirect (and the other actions you want to do after the AJAX call has completed) inside the `xmlhttp.status == 200` if-statement. – Ivar Sep 17 '19 at 13:34
  • Nothing, the problem is that it never loads the function. Indeed, if I try an alert using onload instead of onreadystatechange, it doesn't print it.(I commented the redirect) – Chiara Pellecchia Sep 17 '19 at 15:30

0 Answers0