0

I'm using a JS MVC framework (sammy) for the first time and I'm clueless about what is the preferred way of sending data to the backend. In my case I have a js object with all the data that I need to process and add in the database. It looks something like this -

object = { 1: ['a'], 2: ['b'], .... };

Should I use jQuery's Get (or Post) methods to send this via ajax or is there any other way to do it?

Thanks

1 Answer 1

1

The easiest way is to send the data to the server as JSON, using POST (or PUT), parse the JSON on the server and perform the necessary processing.

e.g. to send the data to the server using jQuery

$.ajax({
    type: 'POST',
    url: 'SaveData.html',
    cache: false,
    dataType: 'json',
    data: object,
    success: function (data) {
       // Add success handling here
    },
    error: function (XMLHttpRequest, textStatus, errorThrown) {
       // Add error handling here
    }
});

On the server you can parse the JSON using a builtin function or third-party library e.g. I use JSON .NET.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.