views:

68

answers:

1

I need to request web page client-side and than pass it to server as a string. I tried jQuery:

$.get(
    "http://example.ru/",
    {name:"Joe", age:"42"},
    function(data){
        $.get(
            "script.php",
            {data:data, query:query},
        )
    });
});

but did not succeed. I suspect it failed because of custom headers added by jQuery.

Can you advice me some technique to override request headers or any js library that makes requests just like browser does?

+3  A: 

You've been caught out by Same Origin Policy:

The same origin policy prevents a document or script loaded from one origin from getting or setting properties of a document from another origin.

What you can do is use a simple proxy on your domain that fetches the page you're interested in (with permission, of course) thus allowing you to display it on your page via ajax requests. What I mean is something like the following:

$.get("yourdomain/proxy.php?name=Joe&age=42"
    function(data){
        $.get(
            "script.php",
            {data:data, query:query},
        )
    });
});
karim79
o_O So there is no way to fetch data from host A by javascript executed on host B? But how such things as google web search api works, in that case?
dir01
You can ask for a javascript file from a <script> tag from a different domain, and exchange data through JSON
Victor
Victor, does it mean that if I use jQuery hosted on the server I want to access to than everything is OK?
dir01