简体   繁体   中英

How to POST/GET/PATCH/DELETE in SensorThings API Using JavaScript/jQuery

I am trying to POST/GET/PATCH/DELETE in SensorThings API as instructed in http://developers.sensorup.com/docs/ . I can experiment succesfully with HTTP (using Postman) and cURL (using bash shell). However, I cannot manage to experiment with JavaScript/jQuery. I don't have any deep experience in both scripting languages. I tried to make a page like this.

<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>SensorThings API Test</title>
</head>
<body>
    <script>
        // I followed the following lines from the http://developers.sensorup.com/docs/ page
        var json = JSON.stringify({
            "name": "Temperature Monitoring System",
            "description": "Sensor system monitoring area temperature",
            "properties": {
                "Deployment Condition": "Deployed in a third floor balcony",
                "Case Used": "Radiation shield"
            }
        }
        });

        $.ajax({
            url: "https://scratchpad.sensorup.com/OGCSensorThings/v1.0/Things",
            type: "POST",
            data: json,
            contentType: "application/json; charset=utf-8",
            success: function(data){
                console.log(data);  
            },
            error: function(response, status){
                console.log(response);
                console.log(status);
            }
        });
    </script>
</body>
</html>

And I run it in the browser, but the console display error messages ((Uncaught SyntaxError: missing ) after argument list)). I think maybe I should insert some of JavaScript code before the tag, but I don't have any idea what should I do. Can someone provide a single page that can make me "POST" a thing into the server?

Thanks in advance...

Your json data is not in valid format, That throws the error. You have entered an extra '}' to the json Correct format is shown below

var json = JSON.stringify({
    "name": "Temperature Monitoring System",
    "description": "Sensor system monitoring area temperature",
    "properties": {
        "Deployment Condition": "Deployed in a third floor balcony",
        "Case Used": "Radiation shield"
    }            
});

And also you didn't added jquery libratry. If its not shown in the code please forget what i said.Else add the jquery library first, Before adding the actual code

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

 <!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>SensorThings API Test</title> </head> <body> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script> // I followed the following lines from the http://developers.sensorup.com/docs/ page var json = JSON.stringify({ "name": "Temperature Monitoring System", "description": "Sensor system monitoring area temperature", "properties": { "Deployment Condition": "Deployed in a third floor balcony", "Case Used": "Radiation shield" } }); $.ajax({ url: "https://scratchpad.sensorup.com/OGCSensorThings/v1.0/Things", type: "POST", data: json, contentType: "application/json; charset=utf-8", success: function(data){ console.log(data); }, error: function(response, status){ console.log(response); console.log(status); } }); </script> </body> </html>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM