Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using JSON POST Request

I'm attempting to use JSON to initiate a POST request to an API.

I've found some example code, and before I get too far I wanted to get that working, but I'm stuck...

<html>
<head>
<script type="text/javascript">
function JSONTest()
{
requestNumber = JSONRequest.post(
    "https://example.com/api/",
    {
        apikey: "23462",
        method: "example",
        ip: "208.74.35.5"
    },
    function (requestNumber, value, exception) {
        if (value) {
            processResponse(value);
        } else {
            processError(exception);
        }
    }
); 
}
</script>
</head>
<body>

<h1>My JSON Web Page</h1>


<button type="button" onclick="JSONTest()">JSON</button>

</body>
</html> 

This is a .html file, which I am running in chrome. Nothing happens when I click the button...

I think I'm missing a piece of javascript which interprets the JSON response and can be displayed? otherwise any other advice?

like image 798
GK1667 Avatar asked Jul 12 '12 16:07

GK1667


People also ask

Can we send JSON in POST request?

POST requests In Postman, change the method next to the URL to 'POST', and under the 'Body' tab choose the 'raw' radio button and then 'JSON (application/json)' from the drop down. You can now type in the JSON you want to send along with the POST request. If this is successful, you should see the new data in your 'db.

How do I send a JSON POST request in python?

To post a JSON to the server using Python Requests Library, call the requests. post() method and pass the target URL as the first parameter and the JSON data with the json= parameter. The json= parameter takes a dictionary and automatically converts it to a JSON string.

What is post method in JSON?

post() method allows you to send asynchronous http POST request to submit and retrieve the data from the server without reloading whole page. Syntax: $.post(url,[data],[callback],[type]) Specify type parameter for the type of response data e.g. specify 'JSON' if server return JSON data.

How do I post JSON to the server?

Use JSON. stringify() to convert the JavaScript object into a JSON string. Send the URL-encoded JSON string to the server as part of the HTTP Request. This can be done using the HEAD, GET, or POST method by assigning the JSON string to a variable.


1 Answers

An example using jQuery is below. Hope this helps

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<title>My jQuery JSON Web Page</title>
<head>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script type="text/javascript">

JSONTest = function() {

    var resultDiv = $("#resultDivContainer");

    $.ajax({
        url: "https://example.com/api/",
        type: "POST",
        data: { apiKey: "23462", method: "example", ip: "208.74.35.5" },
        dataType: "json",
        success: function (result) {
            switch (result) {
                case true:
                    processResponse(result);
                    break;
                default:
                    resultDiv.html(result);
            }
        },
        error: function (xhr, ajaxOptions, thrownError) {
        alert(xhr.status);
        alert(thrownError);
        }
    });
};

</script>
</head>
<body>

<h1>My jQuery JSON Web Page</h1>

<div id="resultDivContainer"></div>

<button type="button" onclick="JSONTest()">JSON</button>

</body>
</html> 

Firebug debug process

Firebug XHR debug process

like image 87
Phil Avatar answered Oct 19 '22 08:10

Phil