Calling External API with Javascript

前端 未结 5 1796
佛祖请我去吃肉
佛祖请我去吃肉 2021-02-14 03:33

I need to make a POST request to an external server from my webpage using Javascript. The body and response are both json. I can\'t figure out how to make this call or what tool

5条回答
  •  借酒劲吻你
    2021-02-14 04:10

    If you use jquery, use .post, or .ajax, to submit

    $.post(url, data, callbackSuccess, callbackError);

    more about these methods here http://api.jquery.com/jquery.ajax/

    example:

    var url = 'http://example.com/path/endpoint';
    
    $.post(url, {name: 'Darlan', lastname: 'Mendonça'}, function(response){
        // callback success
    }, function(response) {
        // callback error
    });
    

提交回复
热议问题