adityakce
adityakce

Reputation: 3

Javascript retrieve json and POST

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.

Upvotes: 0

Views: 58

Answers (1)

Don Rhummy
Don Rhummy

Reputation: 25810

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

Upvotes: 1

Related Questions