Amit
Amit

Reputation: 6304

Custom AJAX dataType type

Is such thing possible?

I want to create a dataType called "json/rows", that parses the text the server outputs, and does something to it, then it goes to the success function?

Example code in how I think it should work:

$.ajax({
    dataType: "json/rows",
    dataTypeParser: function(response) {
        response = JSON.parse(response);
        response.rows = "test";
        return response;
    },
    success: function(response) {
        console.lo(response.rows); //console logs "test"
    }
})

Upvotes: 0

Views: 1400

Answers (1)

Koustav Ray
Koustav Ray

Reputation: 1142

From the jQuery Documentation:

$.ajax({
  accepts: {
    mycustomtype: 'application/x-some-custom-type'
  },

  // Instructions for how to deserialize a `mycustomtype`
  converters: {
    'text mycustomtype': function(result) {
      // Do Stuff
      return newresult;
    }
  },

  // Expect a `mycustomtype` back from server
  dataType: 'mycustomtype'
});

here use json/rows instead of application/x-some-custom-type

Upvotes: 2

Related Questions