簡體   English   中英

使用ajax發送帶有參數的url

[英]send url with parameters using ajax

我想使用ajax發送一個帶有參數的url,下面是我想發送的數據http://sample-domain.com/send-email.php?pid=3224&act=report但它只發送http://sample-domain .com/send-email.php

以下是我的ajax代碼

<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>

並在 setnext.php 中

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

您已經說過您正在使用application/x-www-form-urlencoded格式對數據進行編碼。

但在這里:

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

您只是在鍵和值之間放置了一個=而沒有考慮application/x-www-form-urlencoded數據的任何其他規則。

其中一項規則是&分隔鍵=值對……並且您的數據中有&

您必須正確編碼您的數據。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM