0

I am a newbie to javascript

I would like add a new object to my json file likejson.json

Is it possible to insert new object to json file using javascript or jquery?? I try the following code, but not work

$( document ).ready(function() {
    $.ajax({
        method: "POST",
        url: "ht tps:// mysite.com/json.json",
        dataType: "json",
        contentType: "application/json",
        data: JSON.stringify({ Name: "John", latitude: "111", longitude: "222",status:"R" }),
    })
    .done(function( msg ) {
      alert( "Done: " + msg );
    })
    .fail(function( jqXHR, textStatus ) {
      alert( "Request failed: " + textStatus );
    });
});

but I using postman is very easy to add new object.

Any idea how to use javascript or jquery ajax to insert new object to json file??

Thank you vey much, have a nice day

JessciaLau
  • 95
  • 10
  • 2
    What do you mean "insert new object to JSON file using Postman" ? Postman doesn't manipulate objects or execute JS. All it does is calling APIs and making HTTP requests. Also I'm not sure what this does, but you're probably running into CORS issues (blocked call), issue that Postman does not have – Jeremy Thille May 21 '21 at 07:37
  • If you want to have simple code, you need to read everything and change it in memory as js object and write it back to file. If you want to really just add to that file, you could use a those funky curses function with seek and stuff to find the precise place in the file. At that moment the notion of JSON is gone though. https://stackoverflow.com/questions/17855186/seek-equivalent-in-javascript-node-js – The Fool May 21 '21 at 07:42
  • You cannot change a file on the backend directly from the frontend. Whatever you try to achieve this is the wrong approach – Thallius May 21 '21 at 07:50

0 Answers0