How do I send a cross-domain POST request via JavaScript?

后端 未结 17 2432
说谎
说谎 2020-11-21 05:06

How do I send a cross-domain POST request via JavaScript?

Notes - it shouldn\'t refresh the page, and I need to grab and parse the response afterwards.

相关标签:
17条回答
  • 2020-11-21 05:44

    Keep it simple:

    1. cross-domain POST:
      use crossDomain: true,

    2. shouldn't refresh the page:
      No, it will not refresh the page as the success or error async callback will be called when the server send back the response.


    Example script:

    $.ajax({
            type: "POST",
            url: "http://www.yoururl.com/",
            crossDomain: true,
            data: 'param1=value1&param2=value2',
            success: function (data) {
                // do something with server response data
            },
            error: function (err) {
                // handle your error logic here
            }
        });
    
    0 讨论(0)
  • 2020-11-21 05:45

    If you have access to all servers involved, put the following in the header of the reply for the page being requested in the other domain:

    PHP:

    header('Access-Control-Allow-Origin: *');
    

    For example, in Drupal's xmlrpc.php code you would do this:

    function xmlrpc_server_output($xml) {
        $xml = '<?xml version="1.0"?>'."\n". $xml;
        header('Connection: close');
        header('Content-Length: '. strlen($xml));
        header('Access-Control-Allow-Origin: *');
        header('Content-Type: application/x-www-form-urlencoded');
        header('Date: '. date('r'));
        // $xml = str_replace("\n", " ", $xml); 
    
        echo $xml;
        exit;
    }
    

    This probably creates a security problem, and you should make sure that you take the appropriate measures to verify the request.

    0 讨论(0)
  • 2020-11-21 05:45

    I think the best way is to use XMLHttpRequest (e.g. $.ajax(), $.post() in jQuery) with one of Cross-Origin Resource Sharing polyfills https://github.com/Modernizr/Modernizr/wiki/HTML5-Cross-Browser-Polyfills#wiki-CORS

    0 讨论(0)
  • 2020-11-21 05:48

    One more important thing to note!!! In example above it's described how to use

    $.ajax({
        type     : 'POST',
        dataType : 'json', 
        url      : 'another-remote-server',
        ...
    });
    

    JQuery 1.6 and lower has a bug with cross-domain XHR. According to Firebug no requests except OPTIONS were sent. No POST. At all.

    Spent 5 hours testing/tuning my code. Adding a lot of headers on the remote server (script). Without any effect. But later, I've updated JQuery lib to 1.6.4, and everything works like a charm.

    0 讨论(0)
  • 2020-11-21 05:49

    Check the post_method function in http://taiyolab.com/mbtweet/scripts/twitterapi_call.js - a good example for the iframe method described above.

    0 讨论(0)
  • 2020-11-21 05:49
    1. Create two hidden iframes (add "display: none;" to the css style). Make your second iframe point to something on your own domain.

    2. Create a hidden form, set its method to "post" with target = your first iframe, and optionally set enctype to "multipart/form-data" (I'm thinking you want to do POST because you want to send multipart data like pictures?)

    3. When ready, make the form submit() the POST.

    4. If you can get the other domain to return javascript that will do Cross-Domain Communication With Iframes (http://softwareas.com/cross-domain-communication-with-iframes) then you are in luck, and you can capture the response as well.

    Of course, if you want to use your server as a proxy, you can avoid all this. Simply submit the form to your own server, which will proxy the request to the other server (assuming the other server isn't set up to notice IP discrepancies), get the response, and return whatever you like.

    0 讨论(0)
提交回复
热议问题