views:

29

answers:

3

Hi All, I have such an issue

var url = "index.php?id=123&sid=321";
$.ajax({
   type: "POST",
   url: "some.php",
   data: "name=John&url="+url,
   success: function(msg){
     alert( "Data Saved: " + msg );
   }

Now here we have url which contains an & , but & is used to delimit data variables to be sent to server and so since we have this sign in url it thinks that this is a delimiter, and I am not getting the full url variable.

Can somebody help with a wise solution.

Thank in advance.

+1  A: 
var url = 'index.php?id=123&sid=321';

$.ajax({
    type: "POST",
    url: "some.php",
    name: 'John',
    url: url,
    success: function(msg){
        alert( "Data Saved: " + msg );
 });

It would be better to pack all data with JSON as above, and build the URL server-side as part of whatever script you have handling this AJAX request.

Michael Robinson
Centurion
Fyodor Soikin's answer is the best way to deal with this situation
Michael Robinson
+2  A: 

Pass an object instead of string for the data field:

var url = "index.php?id=123&sid=321";
$.ajax({
   type: "POST",
   url: "some.php",
   data: { name: "John", url: url },
   success: function(msg){
     alert( "Data Saved: " + msg );
   }
});
Fyodor Soikin
and in php to make json_decode right?, otherwise how it will understand that this is an object?
Centurion
No, you don't need to do json_decode. jQuery will pass these fields as POST fields, and your PHP script will be able to read them separately, the same way as you usually read data from form posts - $_POST["name"] and $_POST["url"]
Fyodor Soikin
Thanks a lot !!!
Centurion
+1  A: 

Put your data in a JSON.

data: {id: '123', sid: '321'}
Daniel Moura