0

How can JSON object be posted to a Mocky IO URL using Javascript?

I have tried:

function mocky(req, res) {
    test = JSON.post(
        "http://www.mocky.io/v2/5185415ba171ea3a00704eed",
        {
            method: "POST"
        },
        function (test, value, ex) {
            if(value){
                console.log(value);
            } else {
                console.log(ex);
            }

        }
    );
}
Kreena Mehta
  • 303
  • 1
  • 5
  • 16

2 Answers2

1

After trying various solutions, I finally figured out the one that works like a charm!

Run the following command from the project root directory: npm install request

//Enabling CORS on ExpressJS
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-    Type, Accept");
next();
});

//Making an API call from NodeJS server
// Posting field (json object) to mocky.io
    var request=require('request');
    var mockyPostRequest = {
        url: 'http://www.mocky.io/v2/596a5f03110000920701cd92',
        method: 'POST',
        headers: {
            'Content-Type': 'application/json'
        },
        json: field
    };

    request(mockyPostRequest, function(err, res, body) {
        if (res && (res.statusCode === 200 || res.statusCode === 201)) {
            // Logging the post data on the console
            console.log(body);
        }
    });

I used following as the reference: https://enable-cors.org/server_expressjs.html

Thanks everyone for replying.

Kreena Mehta
  • 303
  • 1
  • 5
  • 16
  • I'd recommend just deleting your question. You didn't even mention that you were using Node.JS or even ExpressJS. The only other answer was someone who mistakenly gave you a jQuery answer. – Dave Chen Jul 18 '17 at 00:03
0

Try this typical jquery ajax call-

var jsonData = {"x":"Apple", "y":"Mango"};

$.ajax({
    url: 'http://www.mocky.io/v2/596a5f03110000920701cd92',
    type: 'POST',
    dataType: 'json',
    data: jsonData, 
    success: function() { alert('POST completed'); }
});
Rajeev Ranjan
  • 3,588
  • 6
  • 28
  • 52
  • The server is throwing error ReferenceError: $ is not defined I have jQuery in my index.html: – Kreena Mehta Jul 15 '17 at 18:47
  • And if I have this code on the client side, it is throwing following error: XMLHttpRequest cannot load http://www.mocky.io/v2/596a5f03110000920701cd92. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:3000' is therefore not allowed access. – Kreena Mehta Jul 15 '17 at 18:54
  • Check your existing code to see what you are using instead of `$` for jQuery. It could be something like `$j` or `$jquery`. – Rajeev Ranjan Jul 16 '17 at 03:48
  • You get this error as you make a cross origin request which is blocked by default for security reasons. Refer these links to dig in further - https://www.html5rocks.com/en/tutorials/cors/ , https://stackoverflow.com/questions/20035101/no-access-control-allow-origin-header-is-present-on-the-requested-resource – Rajeev Ranjan Jul 16 '17 at 04:13