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

后端 未结 17 2780
说谎
说谎 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¶m2=value2',
            success: function (data) {
                // do something with server response data
            },
            error: function (err) {
                // handle your error logic here
            }
        });
    

提交回复
热议问题