1

How to pass the fetched data using $.get() from a URL to a particular URL with query_string that can be retrieved using $_GET() in PHP. I'm using following but not working. I want to pass the fetched data into following parameter http://example.com/data?data=FETECHED_DATA

<?php
$ip_address=$_SERVER['REMOTE_ADDR'];    
echo '<script>
$.get( "http://ipinfo.io/'.$ip_address.'/org", function( data ) {
$.ajax({
  type: "GET",
  url: "http://example.com/data",
  data: data,
  success: success,
  dataType: dataType
});
});
</script>';?>

Well, don't recommended PHP to fetch and send data

11
  • You're using $.get and wrapping $.ajax inside. This is not intended to be used this way. You don't need $.ajax, because $.get is a shortcut for this. And please indent your code properly. Thanks. Commented Jul 5, 2015 at 9:36
  • Can you re-write please Commented Jul 5, 2015 at 9:37
  • data: "data="+data, - should pass your received data as get request but as people said - you need to rewrite it Commented Jul 5, 2015 at 9:43
  • Last thing first: please indent the code in your question (add 4 spaces for every block). This makes the code more readable. The chances are higher someone will help you, if the people can read your code with ease. Commented Jul 5, 2015 at 9:43
  • @cezar, nothing wrong with doing a $.ajax inside a $.get if that's what is required. That snippet is trying to get the "org" of an IP from ipinfo.io and using it in an ajax call to example.com/data with the end result processed by a function (not shown) called success Commented Jul 5, 2015 at 9:46

2 Answers 2

1

I believe this will do what you want

<?php
$ip_address=$_SERVER['REMOTE_ADDR'];    
echo '<script>
$.get("http://ipinfo.io/'.$ip_address.'/org", function(data) {
    $.ajax({
        type: "GET",
        url: "http://example.com/data",
        data: {data : data},
        success: success,
        dataType: dataType
    });
});
</script>';?>

the only change is data: data changed to data: {data: data}

that's a lot of data :p

see this cut down mock up, http://jsfiddle.net/cm8o5dk8/ if you have a decent browser that can show you the network "usage" you'll see that the mockup fails trying to GET http://example.com/data?data=AS15169+Google+Inc.%0A - the %0A comes back from ipinfo.io

Sign up to request clarification or add additional context in comments.

9 Comments

It is not passing the fetched info in the URL example.com/data?data=FETECHED_DATA
My apologies - jQueery changes it's behaviour every minor release, so I'm probably 1000 behaviours behind since last year
url: "http://example.com/" + data, ??? Or stands data in the URL for some example parameter?
just tested - the code I posted is SOUND and works and DOES try to get http://example.com/data?data=FETCHED_DATA - any failure is due to you not having a success function defined or the dataType defined corectly
@cezar - read the $.ajax documentation you linked to, find the section explaining the "data" parameter, you'll see why my code is correct
|
0

Thanks it worked

$.get("http://ipinfo.io/8.8.8.8/org", function(data) {
    $.ajax({
        type: "GET",
        url: "http://example.com/data",
        data: {data : data},
        success: function() {},
        dataType: 'json'
    });
});

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.