简体   繁体   中英

send url with parameters using ajax

I want to send a url with parameters using ajax following is the data I want to senthttp://sample-domain.com/send-email.php?pid=3224&act=report but it sends only http://sample-domain.com/send-email.php

Following is my ajax code

<script>
  window.addEventListener("load", function(){

    var x=document.getElementsByTagName("a");
    for(var i=0; i<x.length; i++){
      x[i].addEventListener("click", nextlocation);
    };
  });

  function nextlocation(evt){
     var y = String(this.getAttribute("href"));
     alert(y);
     httpRequest = new XMLHttpRequest();
     httpRequest.open('POST', 'setnext.php');
     httpRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
     httpRequest.send('next='+y);
  };



</script>

and in setnext.php

<?php
session_start();
$_SESSION['next']=$_POST['next'];
httpRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');

You've said that you are encoding the data using the application/x-www-form-urlencoded format.

But here:

 httpRequest.send('next='+y);

You are just putting an = between the key and the value without taking into consideration any of the other rules for application/x-www-form-urlencoded data.

One of those rules is that & separates key=value pairs … and you have a & in your data.

You have to encode your data properly.

 httpRequest.send('next=' + encodeURIComponent(y));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM