Can not receive server response by using ajax

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • tadasyan
    New Member
    • Mar 2016
    • 1

    Can not receive server response by using ajax

    I want to use ajax to fill input fields depending on the select element

    Code:
    <select name="service" onchange="fillFields(this.options[this.selectedIndex].value)">
    Js code looks like:

    Code:
    function fillFields(inc){
    var xmlhttp = new XMLHttpRequest();
    xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            var data = JSON.parse(xmlhttp.responseText);
            document.getElementById("datepicker").innerHTML = data.datebeg;
            document.getElementById("ph").innerHTML = data.phone;
            var form = document.forms.mainForm;
            var towns = form.elements["towns[]"];
            var hours = form.elements["hours[]"];
            var minutes = form.elements["minutes[]"]
            var i;
            for (i = 0; i < data.towns.length; i++){
                towns[i] = data.towns[i];
                hours[i] = data.hours[i];
                minutes[i] = data.minutes[i];
            }
        }
    };
    xmlhttp.open("GET", "fillForm.php?inc=" + inc, true);
    xmlhttp.send();
    }
    fillForm.php retrieves data from db and sends response in JSON format.

    It seems to me, I do everything exactly like in examples, but (xmlhttp.readyS tate == 4) condition fails. ReadyState equals to one. What is my problem?
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    if you only ever get a readyState of 1 (and not cancelling the process) then either line 21 produces an error or your server doesn’t respond at all.

    Comment

    Working...