0

whenever i use jquery ajax for get and post then i wrote the code as follows

$(document).ready(function () {
$.ajax({
type: "POST",
url: "MyPage.aspx/ProcessInfo",
data: JSON.stringify(DTO),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {}

});

so i need to know that how could i write a generic routine for jquery ajax call where i can pass request type call GET/POST, URL, DATA TYPE, and DATA. data could be in json format or may be like two or three augument. so help me with concept by which i can write generic routine which i can use in any situation for ajax call. where data type is json or not. thanks

1
  • why would you want a different method where you still have to pass in the same information? this makes no sense to me... I could understand building a shorthand where you don't have to pass in the information, however you could simply set the defaults using ajaxSetup... this seems pretty useless. Commented Oct 19, 2011 at 19:35

4 Answers 4

4

You could use jQuery's build int $.get() and $.post() shorthand or, create your own custom wrapper:

$(document).ready(function() {
    function jQuerySubmit(type, url, data, contentType, dataType, handler) {
        $.ajax({
            type: type,
            url: url,
            data: data,
            contentType: contentType
            dataType: dataType,
            success: handler
        });
    }
});
Sign up to request clarification or add additional context in comments.

1 Comment

can i call jQuerySubmit straight way like jQuerySubmit("POST","MyPage.aspx/ProcessInfo",DTO,"application/json; charset=utf-8","json",myfunc)
3

Instead of writing a function for doing something simple like an ajax request, why not overwrite the defaults in the $.ajax object.

For example:

If your contentType will always be "application/json; charset=utf-8" you could set that as the default with the $.ajaxSetup method like this:

jQuery.ajaxSetup({
     contentType: "application/json; charset=utf-8",
     dataType: "json",
     type: "POST"
});

Then you could write your ajax calls with less parameters:

$(document).ready(function () {
    $.ajax({
    url: "MyPage.aspx/ProcessInfo",
    data: JSON.stringify(DTO),
    success: function (data) {}
});

If you want to create a function to do this, I would still advise to setup your defaults but you could do something like any of the other answers.

Comments

1

It already designed in jQuery:

GET: http://api.jquery.com/jQuery.get/

$.get("test.php",
   function(data){
     $('body').append( "Name: " + data.name ) // John
              .append( "Time: " + data.time ); //  2pm
   }, "json");

POST: http://api.jquery.com/jQuery.post/

$.post("test.php", { "func": "getNameAndTime" },
  function(data){
    console.log(data.name); // John
    console.log(data.time); //  2pm
  }, "json");

Comments

0
function makeAjax (type , url , data , content,dataType , succFunc)
{
$.ajax({
type: type ,
url: url ,
data: JSON.stringify(data ),
contentType: content,
dataType: dataType ,
success:succFunc(data)

});
}

makeAjax ("POST","MyPage.aspx/ProcessInfo",DTO,"application/json; charset=utf-8","json",myfunc)

function succFunc(data)
{
}

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.