How can I make a custom "POST" request on client side?

markzzz

I need to do a POST request such as:

POST /feeds/api/users/default/subscriptions HTTP/1.1
Host: gdata.youtube.com
Content-Type: application/atom+xml
Content-Length: CONTENT_LENGTH
Authorization: Bearer ACCESS_TOKEN
GData-Version: 2
X-GData-Key: key=DEVELOPER_KEY

<?xml version="1.0" encoding="UTF-8"?>
<entry xmlns="http://www.w3.org/2005/Atom"
  xmlns:yt="http://gdata.youtube.com/schemas/2007">
    <category scheme="http://gdata.youtube.com/schemas/2007/subscriptiontypes.cat"
      term="channel"/>
    <yt:username>GoogleDevelopers</yt:username>
</entry>

I know how to do it server (.NET/C#) side for example, using HttpWebRequest object, settings Header/Method/ContentType.

But if I'd like to do it client side? Ajax with jQuery? Where can I set those parameters?

jcubic

You can use this function:

function post(url, data, headers, success) {
    $.ajax({
        beforeSend: function(xhr){
            $.each(headers, function(key, val) {
                xhr.setRequestHeader(key, val);
            });
            xhr.setRequestHeader('Content-Length', data.length);
        }
        type: "POST",
        url: url,
        processData: false,
        data: data,
        dataType: "xml",
        success: success
    });
}

using code like this:

var request = '<?xml version="1.0" encoding="UTF-8"?>' +
       '<entry xmlns="http://www.w3.org/2005/Atom"' +
       '        xmlns:yt="http://gdata.youtube.com/schemas/2007">' +
       '    <category scheme="http://gdata.youtube.com/schemas/2007/subscriptiontypes.cat" term="channel"/>'+
       '    <yt:username>GoogleDevelopers</yt:username>' +
       '</entry>';

var headers = {
   'Content-Type': 'application/atom+xml',
   'Authorization': 'Bearer ACCESS_TOKEN'
   'GData-Version': 2
   'X-GData-Key': 'key=DEVELOPER_KEY'
};

post('/some/url', request, headers, function(response) {
   alert(response);
});

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

Can I save client side state in javascript?

분류에서Dev

how can I make thread in TCP server multi client dialogue

분류에서Dev

Two divs side by side: how can i make the div that is on the left side go down when it breaks for responsive layout?

분류에서Dev

How can I make this custom filter work in django?

분류에서Dev

How to stop server process during a post on client side?

분류에서Dev

How to a variable can manage all client request

분류에서Dev

How can I trace out entire GET request from jersey api client call

분류에서Dev

how can i synchronize wordpress between local side and remote side?

분류에서Dev

Firebase, React: How do I display http errors on client side?

분류에서Dev

How can i manage this request?

분류에서Dev

How can I block if a request contains a word in get url or post body using urlrewrite for tomcat

분류에서Dev

How can I make a c# https web request that works in both proxy and no proxy situations

분류에서Dev

how to make a faked Android/ios http post request from desktop

분류에서Dev

How to make cross-domain ajax Post request using jquery?

분류에서Dev

How can i get request in Filter

분류에서Dev

How can i have a multiselect option in right side end

분류에서Dev

How can i use $_POST if the name is a variable?

분류에서Dev

How Can I Create A Custom Property Type

분류에서Dev

How Can I Create A Custom Property Type

분류에서Dev

how can I design custom toast message?

분류에서Dev

How can I line up multiple tables side by side in Outlook email?

분류에서Dev

How can I make IBus not ignore ~/.XCompose?

분류에서Dev

How can i make this css star smaller?

분류에서Dev

How can I make export permanent?

분류에서Dev

How can I make a functioning animated button?

분류에서Dev

How can I make this JButton work

분류에서Dev

How can I make this JButton work

분류에서Dev

How can I make this cases match with regex?

분류에서Dev

How can I partially serialize with GNU make

Related 관련 기사

  1. 1

    Can I save client side state in javascript?

  2. 2

    how can I make thread in TCP server multi client dialogue

  3. 3

    Two divs side by side: how can i make the div that is on the left side go down when it breaks for responsive layout?

  4. 4

    How can I make this custom filter work in django?

  5. 5

    How to stop server process during a post on client side?

  6. 6

    How to a variable can manage all client request

  7. 7

    How can I trace out entire GET request from jersey api client call

  8. 8

    how can i synchronize wordpress between local side and remote side?

  9. 9

    Firebase, React: How do I display http errors on client side?

  10. 10

    How can i manage this request?

  11. 11

    How can I block if a request contains a word in get url or post body using urlrewrite for tomcat

  12. 12

    How can I make a c# https web request that works in both proxy and no proxy situations

  13. 13

    how to make a faked Android/ios http post request from desktop

  14. 14

    How to make cross-domain ajax Post request using jquery?

  15. 15

    How can i get request in Filter

  16. 16

    How can i have a multiselect option in right side end

  17. 17

    How can i use $_POST if the name is a variable?

  18. 18

    How Can I Create A Custom Property Type

  19. 19

    How Can I Create A Custom Property Type

  20. 20

    how can I design custom toast message?

  21. 21

    How can I line up multiple tables side by side in Outlook email?

  22. 22

    How can I make IBus not ignore ~/.XCompose?

  23. 23

    How can i make this css star smaller?

  24. 24

    How can I make export permanent?

  25. 25

    How can I make a functioning animated button?

  26. 26

    How can I make this JButton work

  27. 27

    How can I make this JButton work

  28. 28

    How can I make this cases match with regex?

  29. 29

    How can I partially serialize with GNU make

뜨겁다태그

보관