0

To avoid duplicate codes, I plan to write OOP. It's a single page app, it has many operation performed by users. For example CRUD (create, read, update & delete).

                var dltTask = $.ajax({
                    url: "process.php",
                    type: "POST",
                    data: {
                        insert: "something"
                    },
                    dataType: "text"
                });

                dltTask.done(function (msg) {
                    alert(msg);
                });

How to pass argument to ajax as object? for example in my insert,

insert = new ajax(insert, data1,data2,data3);

4
  • data: { insert: "", data1: data1, data2: data2 } Why don't u use like this? Commented Dec 8, 2013 at 4:53
  • because I have to write $.ajax many time. for example create, update, delete, 3 times.. duplication Commented Dec 8, 2013 at 5:01
  • Use jQuery.param( params ); Commented Dec 8, 2013 at 5:04
  • 1
    Write a wrapper function then. Commented Dec 8, 2013 at 5:04

1 Answer 1

1

you can pass values like that

postval = {
    data1: data1,
    data2: data2,
    data3:data3
}

var dltTask = $.ajax({
                    url: "process.php",
                    type: "POST",
                    data: postval,
                    dataType: "text"
                });

                dltTask.done(function (msg) {
                    alert(msg);
                });
Sign up to request clarification or add additional context in comments.

5 Comments

I still need to write multiple times of the second block. this is not oop still.
overloaded if I pass unnecessary data, for example insert only need 3, I pass more than that.
unnecessary data means what and in postval you can pass more data, it will work. how u r passing php data. can u show me.
use if. for insert : if!empty($_POST['insert']) for update if!empty($_POST['updt'])..
From single line we can not understand ur problem. show ur more code then we can resolve ur pbm.

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.