There is a request that is sent to a foreign site and transmits the value of the variable gett via the URL, the foreign site knows nothing about me. Then parsim content from the diva (what is in the callback). After the success request is not executed, although in the Chrome debugger you can see the page, swears at the Unexpected token. If I add error, it will go to error after executing the request.

function jsonp_callback(){ $('#what_i_want').val(); } $.ajax({ dataType: 'jsonp', jsonp: 'jsonp_callback', url: 'http://domen.ru/'+$('#my_input').val()+'.html', success: function (data) { alert('jsonp'); } }); 

What is wrong doing? I read the docks and did not fully understand whether JSONP is suitable in my case or not, in the sense that the FOREIGN site "does not know me" and does not give me any JSON server.



    1 answer 1

    Requests from a client to another domain are denied by the JavaScript security policy.

    The solution is to make a request to your domain, where a proxy script is located that makes a request to a remote resource and returns the result to you.


    New versions of browsers allow cross-domain query, but the server should, as you said, “know” about you and return the Access-Control-Allow-Origin header, which contains a specific domain, or *


     $data=http_build_query($_POST); $contx=stream_context_create(array('http'=>array( 'method'=>'GET', 'header'=>'content-Type: application/x-www-form-urlencoded; charset=UTF-8', 'content'=>$data ))); readfile("http://$_POST[addr]",false,$contx); 

    I used this code to transfer the post request to another server. You send a post (you can get ) to the script, it throws it to the $ _POST ['addr'] method specified in $contx . Correspondingly, your addr parameter must be specified in your AJAX request.

    • if necessary I can provide this for PHP - Gedweb am
    • Yes, I’m happy to see your decision - deniz
    • 2
      the only disadvantage of the method is that all requests are made via (usually one) server ip, on which the host is located - Gedweb
    • one
      That's about it, I also wondered how many requests Yandex would allow to do before banning my ip ?! - deniz