Using ajax to make a curl request

前端 未结 3 369
逝去的感伤
逝去的感伤 2021-01-25 05:51

This answer has helped me largely but hasn\'t fully resolved my issues. I\'m using code similar to that, which is:

function getResponse() {

    var the_url = \"         


        
相关标签:
3条回答
  • 2021-01-25 06:26

    Update your jquery ajax call to include the username and passward options for HTTP authentication, see the Advanced options in the documentation:

    If the server performs HTTP authentication before providing a response,
    the user name and password pair can be sent via the username 
    and password options.
    

    Update as pointed out in the comments, HTTP authentication simply doesn't work with JSONP, as the questioner used (didn't see that, my bad). I'll leave this answer here anyway for other people to find that don't use JSONP.

    0 讨论(0)
  • 2021-01-25 06:39

    If you were using simple AJAX, then you could just add custom (basic authentication) header at the begining of your AJAX call. But you are using JSONP, which actually is equivalent to adding <script> tag to your HTML.

    So what you are asking is: can I do basic authentication with <script> tag? No, you can't.

    The only possibility left for you is to do server side processing, like Matt Gibson suggested.

    Note however, that the browser should fire authentication dialog after the JSONP request (also see this answer). Isn't it working in your case??

    0 讨论(0)
  • 2021-01-25 06:41

    You need a page on your server that the JavaScript points at, which has the curl command in it. You can't just run cURL directly as a url like that.

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $the_url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_REFERER, $_SERVER['REQUEST_URI']);
    $result = curl_exec($ch);
    curl_close($ch);
    echo $result;
    
    0 讨论(0)
提交回复
热议问题