Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to send a url using Javascript ajax? [duplicate]

Possible Duplicate:
How to encode a URL in JavaScript?

I am trying to send a url using the following code to a php code, but as the url include &a=12&b=4 once I get the value of the "a" variable in my php code the last part of address is removed.

url = http://www.example.com/help.jpg?x=10&a=12&b=4 but the url that I get in my php file is http://www.example.com/help.jpg?x=10 (&a=12&b=4 is removed, I know the reason is that javascript,ajax mix it up with the url address and do not know its just a value but do not know how to solve it)

         function upload(url){

            if (window.XMLHttpRequest)
            {// code for IE7+, Firefox, Chrome, Opera, Safari
                xmlhttp=new XMLHttpRequest();
            }
            else
            {// code for IE6, IE5
                xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
            }
            xmlhttp.onreadystatechange=function()
            {
                if (xmlhttp.readyState==4 && xmlhttp.status==200)
                {
                    document.getElementById("output").innerHTML= xmlhttp.responseText;
                }
            }
            xmlhttp.open("GET","Photos.php?a="+url,true);
            xmlhttp.send();
     }        


   if(isset($_GET["a"]))
   {
       $Address = $_GET["a"];
       echo $Address;

   }

output is >>> " http://www.example.com/help.jpg?x=10" but it should be http://www.example.com/help.jpg?x=10&a=12&b=4

like image 699
Saeed Pirdost Avatar asked Jan 07 '13 21:01

Saeed Pirdost


People also ask

Can we send two URLs in AJAX?

You can't call 2 url simultaneously. but you can call one after another. You can't put two URLs in an ajax call.

Which method is used to create HTTP request using JavaScript in AJAX?

To make an HTTP call in Ajax, you need to initialize a new XMLHttpRequest() method, specify the URL endpoint and HTTP method (in this case GET).

Which jQuery method is used to send a request to the API URL?

The ajax() method in jQuery is used to perform an AJAX request or asynchronous HTTP request. Parameters: The list of possible values are given below: type: It is used to specify the type of request. url: It is used to specify the URL to send the request to.


1 Answers

You need to encode the parameter

xmlhttp.open("GET","Photos.php?a="+encodeURIComponent(url),true);
like image 54
Musa Avatar answered Oct 28 '22 05:10

Musa