Question

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?

Was it helpful?

Solution

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},
        )
    });
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top