0

I am trying to send a AJAX request externally but it is not working, it works locally and all but as soon as i try and link it too an external webpage it does not do anything.

I have done some reading and i read that their is something called the same origin policy which does not allow access due to secruity reasons.

Which then made me read into jsonp but that gave me a syntax error

I am just trying to display information from my database which is not in any format such as JSON or XML.

Any help would be appreciated

Jquery

    var dataString = 'gender=' + gender + '&Status=' + status + '&Lovethem1=' + lovethem + 
    '&Lovethem2=' + lovethem2 +'&Arrays=' + vals;  

$.ajax({
    type: "GET",
    url: 'http://transformer.tamar.com/test.php',
    jsonp: 'jsonp',
     jsonpCallback: "myJsonMethod",
     data: dataString,  
    success: function (result) {
   var div = document.getElementById('update');
   $('#update').show();
   div.innerHTML = result;
   //process the json here.
    }
});
Liam
  • 27,717
  • 28
  • 128
  • 190
Hashey100
  • 994
  • 5
  • 22
  • 47
  • 1
    JSONP is the dataformat used by the server, just setting the options in the ajax call to JSONP won't magically work, and neither will cross domain calls if the server you're contacting doesn't support it. – adeneo Nov 25 '13 at 14:48
  • 1
    Is the domain from which you are making this call also http://transformer.tamar.com? If not, you are in violation of the same-origin policy. – marteljn Nov 25 '13 at 14:48
  • 1
    ah, this old chestnut again.... – Liam Nov 25 '13 at 14:49
  • possible duplicate of [jQuery AJAX cross domain](http://stackoverflow.com/questions/3506208/jquery-ajax-cross-domain) – Liam Nov 25 '13 at 14:49

2 Answers2

0

If the server is not returning JSON data and you can't use it with JSONP, then your best option is a server-side solution.

Create a small PHP script or web-app in whatever language you like that will make an HTTP request to the remote server, retrieve the results, and send them back to a request you made to it. This of course has to be local so your AJAX JS code can use it under Same Origin policy. There's a great example of a bridge/proxy script here: Remote POST request with jQuery and Ajax

Community
  • 1
  • 1
nemik
  • 658
  • 6
  • 5
0

You can create simple php script to avoid same origin policy, place it locally and doing ajax request to this local file.

For example create new php file:

/test.php

$queryString = $_SERVER['REQUEST_URI'];
echo file_get_contents('http://transformer.tamar.com'.$queryString);

Now you can do ajax locally (to this new php script which will do all the work)

var dataString = 'gender=' + gender + '&Status=' + status + '&Lovethem1=' + lovethem + 
'&Lovethem2=' + lovethem2 +'&Arrays=' + vals;  

$.ajax({
    type: "GET",
    url: '/test.php',
    data: dataString,  
    success: function (result) {
       var div = document.getElementById('update');
       $('#update').show();
       div.innerHTML = result;
       //process the json here.
    }
});
V G
  • 1,225
  • 9
  • 13
  • i dont understand can you elaborate a bit more please – Hashey100 Nov 25 '13 at 14:59
  • Hashey100, create and visit test.php I suggest, it's kind of proxy. You can do ajax to the new test.php file instead of remote url. – V G Nov 25 '13 at 15:03