0

Hello guys i am trying to retrieve data in json format which is ok. But i am having reposting the data to another url.

<script id="source" language="javascript" type="text/javascript">

  $(function () 
  {
    $.ajax({                                      
      url: 'api.php',                  //the script to call to get data          
      data: "",                        //you can insert url argumnets here to pass to api.php for example "id=5&parent=6"
      dataType: 'json',                //data format      
      success: function(data)          //on recieve of reply
      {
      var formData = {fname:data[0],lname:data[1],email:data[2]};
    //---------------------------------------------------------------------
    });
    $.ajax({
    url : "http://requestb.in/1k8rvk71",
    type: "POST",
    data : formData,
    success: function(data, textStatus, jqXHR)
    {
     //data - response from server
    },
    error: function (jqXHR, textStatus, errorThrown)
    {

    }
});
  }); 
  </script>

Plz help guys.. really need to get this work.

adityakce
  • 3
  • 1

1 Answers1

1

You cannot use ajax to send data to a different domain than the one your javascript code is served from.

This is called the Same Origin Policy

There are two possible ways around this restriction:

  1. On your server, have a server-side page (in PHP for example) that takes the data and posts it to that remote URL

  2. If that remote URL has a REST API, you can use JSONP to submit it client-side

  3. CORS - but there's a lot of issues with this cross-browser and server

Don Rhummy
  • 24,730
  • 42
  • 175
  • 330
  • @Phil No, I understand I just had a mental block for a second. already updated – Don Rhummy Dec 16 '14 at 22:27
  • Regarding #3, what issues? – Phil Dec 16 '14 at 22:27
  • @Phil "If the server does not allow the cross-origin request, the browser will deliver an error to example-social-network.com page instead of the online-personal-calendar.com response." A lot of servers reject it, so not good to rely on this. – Don Rhummy Dec 16 '14 at 22:27
  • That entirely depends on the server but in this case you are correct, requestb.in does not set the `Access-Control-Allow-Origin` response header though the request still reaches the server and is logged so I'm not sure if that counts as a success or failure – Phil Dec 16 '14 at 22:32