How to create a POST with request in php or javascript

advertisements

how to create a post with request in php or javascript for steam web API

Example post: https://api.steampowered.com/IEconService/CancelTradeOffer/v1/?key=STEAM_API_KEY&tradeofferid=TRADE_OFFER_ID

when i use it in a browser i get: Method Not Allowed This API must be called with a HTTP POST request

In C# this was written as:

    private bool CancelTradeOffer(ulong tradeofferid)
    {
        string options = string.Format("?key={0}&tradeofferid={1}", ApiKey, tradeofferid);
        string url = String.Format(BaseUrl, "CancelTradeOffer", "v1", options);
        Debug.WriteLine(url);
        string response = SteamWeb.Fetch(url, "POST", null, null, false);
        dynamic json = JsonConvert.DeserializeObject(response);

        if (json == null || json.success != "1")
        {
            return false;
        }
        return true;
    }


If you are using jQuery then there is a very handy function to do this.

$.post( "http://api.example.com/get-some-value", { name: "John", time: "2pm" })
  .done(function( data ) {
    alert( "Data Loaded: " + data );
  });

But be careful about cross domain ajax when calling it from JS.

EDIT

For the comment.

You have to include jQuery into your page and then you can call anything within the very useful and handy $( document ).ready() that jQuery supplies.

$(document).ready(function(){
     $.post( "http://api.example.com/get-some-value", { name: "John", time: "2pm" })
      .done(function( data ) {
        alert( "Data Loaded: " + data );
      });
})