Ajax returning Undefined

Asked

Viewed 428 times

1

I put an Else in my php function and it’s falling into Else, but I have parameters to get from the url.

I have the following code in php:

$utm_source = $_REQUEST['utm_source'];
$utm_campaign = $_REQUEST['utm_campaign'];
$utm_medium = $_REQUEST['utm_medium'];
if($utm_source != '' || $utm_campaign != '' || $utm_medium != '')
{
    $x['x'] = $utm_source;
    $x['y'] = $utm_campaign;
    $x['k'] = $utm_medium;
    echo json_encode($x);
}

When I access the file directly through the browser with the parameters ? utm_source=xesquedele&utm_medium=site&utm_Campaign=partners it returns me:

{"x":"xesquedele","y":"parceiros","k":"site"}

But when I try to return it to an ajax it does not return me anything or Undefined.

$j(document).ready(function()
{
    $j.ajax({
        url: '/inchoo_quoteitemrule/ajax/sessiondesconto',
        method: "POST",
        success: function(retorno)
        {
            console.log(retorno);
            alert('utm_medium: '+retorno['k']+' utm_source: '+retorno['x']+' utm_campaign: '+retorno['y']);
        }
    });
});

Return ajax: utm_medium: Undefined utm_source: Undefined utm_campaign: Undefined

  • Ever tried to put dataType:"json", in Ajax?

  • I posed, I managed to resolve the issue, but I had to use other methods.

1 answer

1

The superglobal $_REQUEST returns information from superglobals $_GET, $_POST and $_COOKIE. In your first example you are sending this data via GET, I mean, by the url. And everything worked fine, since the data was being sent. Already in your second example you are not populating any of the 3 superglobals. Your ajax code will work if you order it this way

$j(document).ready(function() {
    $j.ajax({
        url: '/inchoo_quoteitemrule/ajax/sessiondesconto',
        method: "POST",
        data:{utm_source: 'xesquedele',utm_medium:'site',utm_campaign:'parceiros'},
        success: function(retorno) {
            console.log(retorno);
        }
    });
});

Note that I am sending the data in the AJAX request via post. and the data return perfectly.

Remember that your Alert is not working because you are returning a string from your server. You need to turn this string into a Javascript object. To do this you can use the function jQuery.parseJSON(). It would look something like:

$j(document).ready(function() {
    $j.ajax({
        url: '/inchoo_quoteitemrule/ajax/sessiondesconto',
        method: "POST",
        data:{utm_source: 'xesquedele',utm_medium:'site',utm_campaign:'parceiros'},
        success: function(retorno) {
            var obj = jQuery.parseJSON(retorno);
            console.log(retorno);
            alert('utm_medium: '+obj.k+' utm_source: '+obj.x+' utm_campaign: '+obj.y);
        }
    });
});
  • I cannot pass these values on "hand", I would have to take them dynamically according to the url and I cannot use a $_REQUEST and return it to php pq will cache.. The way will be I take by jquery the link and break it taking the values I would like, thank you.

  • It’s an option to do this.

  • I looked for a function for that. var url = Location.href.substring(Location.href.lastIndexOf('?') + 1) but then I’ll have to break the string and return, but at least it won’t cache..

Browser other questions tagged

You are not signed in. Login or sign up in order to post.