Retrieve URL variable and forward in AJAX

2

I would like to know if you have how to retrieve a variable from the URL to another page and return the result in load . I am using the following script that normally retrieves the variable, but does not move to the next page. It's just to understand better what I need.

In URL http://noticias.php?categoria=23 , I get the variable normally but in load comes the information that the resposta.php page did not receive the variable. I would like to move to another page before showing the result in load . What would be the correct way?

$(document).ready(function()
{
    $('#teste').html("<img src='js/load.gif'>");
    var variaveis=location.search.split("?");
    var quebra = variaveis[1].split("=");
    var categoria = + quebra[1];
    var URL='resposta.php';
    var dataString = 'categoria=' + categoria ;

    $.ajax({
        type: "POST",
        url: URL,
        data: dataString,
        cache: false,
        success: function(html){
            $('#teste').load('resposta.php');
        }
    });
});
</script>
    
asked by anonymous 11.07.2014 / 19:18

2 answers

1

The correct way to send to the data parameter is this way.

Jquery

$.ajax({
    type: "POST",
    url: URL,
    data: categoria,
    cache: false,
    success: function(html){
       $('#teste').load('resposta.php');
    }
});

or in the form of a Array

Jquery

$.ajax({
    type: "POST",
    url: URL,
    data: { meuVar : categoria },
    cache: false,
    success: function(html){
       $('#teste').load('resposta.php');
    }
});

And in PHP would receive this way

PHP

$categoria = $_POST["meuVar"];
    
11.07.2014 / 19:28
1

I do not know if this helps either, but to pass the value by get with $ .ajax like this:

var URL='resposta.php?'+dataString;

$.ajax(
{
url: URL,
cache: false,
success: function(html){
    $('#teste').load('resposta.php');
}
});

If I'm not mistaken, you can get the type of $ .ajax pq by default it already works with GET.

    
11.07.2014 / 19:46