How to put & in in URL with jQuery.load?

I have a WordPress page that should load some code from the server using jQuerys load method:

<div id="mydiv"></div>
<script>
jQuery("#mydiv").load("http://localhost/testserver/wp-content/plugins/myplugin/scripts/view/test.php?x=1&amp;y=2&amp;z=3");
</script>

My test.php script is:

Read More
<?php error_log(var_export($_GET,true)); ?>

But I am unable to get the parameters x, y, z correctly in the test.php script: The output in the error log shows:

'x' => '1',
'amp;y' => '2',
'amp;z' => '3',

(If I use x=1&y=2&z=3 in the url string, I just get ‘x’ => ‘1’, in the error log).

Am I doing something wrong or could this be a WordPress or jQuery bug?

Related posts

Leave a Reply

2 comments

  1. Use encodeURIComponent();

    Example

    var url = "http://localhost/testserver/wp-content/plugins/myplugin/scripts/view/test.php?x=1&y=2&z=3"
    url = encodeURIComponent(url);
    
    jQuery("#mydiv").load(url);
    

    Alternatively use .get()

    jQuery.get('http://localhost/testserver/wp-content/plugins/myplugin/scripts/view/test.php', {x: 1, y:2, z:3}, function(r){
       jQuery("#mydiv").html(r);
    });