0

So I have a php file called paste.php in a directory in a server with domain example.com (http://www.example.com/ajax/paste.php) The php file looks like:

<?php
$id=($_GET['create']);
$paste=($_GET['paste']);
$toly=($_GET['toly']);
if($id=='create'){
echo "hello";}
else{
echo "world";
} ?>

In another server with different domain i have a simple html file (www.example/demo/index.html) I 'am trying to make an ajax call to the php file above with no luck My jquery looks like:

var url="http://www.example.com/ajax/paste.php";
s="create=create&paste=hello&toly=this";
$.ajax({
   type: "GET",
    xhrFields: {
      withCredentials: true
   },
   url: url,
   data: s,
   success: function(msg){
   alert(msg);
   },error:function (jqXHR){
                    alert(jqXHR.status.error);
                }  
 }); 

What am I doing wrong?

EDIT : Forget the xhr i just want to get back the string i have made as a response Is there any way?

arserbin3
  • 6,010
  • 8
  • 36
  • 52
vorillaz
  • 6,098
  • 2
  • 30
  • 46

3 Answers3

2

From JavaScript you can create a new element dynamically inside document's head. When you do that, browsers can go and request data from any domain you specify there. If the called code contains execution of certain function, than that function will be able to link to your code. Here is example:

    var head = document.getElementsByTagName("head")[0];
    var script = document.createElement('script');
    script.type = 'text/javascript';
    script.src = url;
    head.appendChild(script);

You would also need to define a function inside your script:

    function success(abc){
        alert('Yey'+abc);
    }

The script form the other domain should look like this:

    success('some_data_here');

That data could also be JSON. When JSON is combined with a function call like that, it's called JSONP.

Read further here:

http://en.wikipedia.org/wiki/JSONP

romaninsh
  • 10,606
  • 4
  • 50
  • 70
1

You cannot do cross domain XHR unless you do Cross Origin Resource Sharing. The same origin policy prevents you.

Paul Grime
  • 14,970
  • 4
  • 36
  • 58
0

You could use a serverside proxy or code your service to support JSONP.

http://devlog.info/2010/03/10/cross-domain-ajax/

EDIT: Oh, yeah, look at the one comment. Check out the other thread.

Joshua Evensen
  • 1,544
  • 1
  • 15
  • 33