Harikrishna Gannu
Harikrishna Gannu

Reputation: 41

Setting 'None' instead of required data

I want to set a field value based on the value of another field(Using AJAX). Here, I am trying to set the same value but it is getting set to 'None' instead of the actual value.

script for sending a request :

<script>
  function getCustomerName() {
    var x = document.ipwhitelistindex.AWSID.value;
    console.log(x)
    var xhttp = new XMLHttpRequest();
    xhttp.onreadystatechange = function() {
      if (this.readyState == 4 && this.status == 200) {
        document.ipwhitelistindex.CUSTNAME.value = this.responseText;
      }
    };
    xhttp.open('POST', 'getcustomernamefromexcel', true);
    xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xhttp.send(x);
  }
</script>

Here, I am sending x. I can see the exact value in the console but it is setting to None on the page.

python :

 def getcustomernamefromexcel(request):
    value = request.POST.get('x')
    return HttpResponse(value)

Upvotes: 1

Views: 50

Answers (1)

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 477308

If you call xhttp.send(x), then the name of the variable is no longer known. You need to encode this, for example with:

xhttp.send("x="+encodeURIComponent(x));

Upvotes: 1

Related Questions