Author Topic: How to use dataModel.method = "POST"  (Read 8106 times)

nghiemhd

  • Newbie
  • *
  • Posts: 16
    • View Profile
How to use dataModel.method = "POST"
« on: September 30, 2013, 09:37:04 am »
Hi all,
In the page ClaimPortalPage.aspx I have a method like this

[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public static string GetMyClaimsWithSearch(string caseManagerId, int selectedPage, int pageSize, string sortBy, string searchCondition)
{
   Get data and return json type
}

I use dataModel as below but I can't call GetMyClaimsWithSearch method, it's always call Page_Load()

dataModel = {
 location: "remote",
 method: "POST",           
 getUrl: function () {
               var data = '{ "caseManagerId":"' + caseManagerId + '"'
                    + ', "selectedPage":"' + this.curPage + '"'
                    + ', "pageSize":"' + this.rPP + '"'
                    + ', "sortBy":"' + sortIndx + "-" + sortDir + '"'
                    + ', "searchCondition":"' + claimSearchCondition + '"'
                    + ' }';
                var getObj = {
                    url: "/PortalPage/ClaimPortalPage.aspx/GetMyClaimsWithSearch",
                    data: data
                };
                return getObj;
            },
            getData: function (dataJSON) {                               
                return { curPage: dataJSON.SelectedPage, totalRecords: dataJSON.TotalRecord, data: dataJSON.data };
            }
        };

My question is how to call GetMyClaimsWithSearch method like jquery ajax post?

$.ajax({
 type: "POST",
 url: "/PortalPage/ClaimPortalPage.aspx/GetMyClaimsWithSearch",
 data: data,
 contentType: "application/json; charset=utf-8",
 dataType: "json",
 success: GetDataSuccess,
 error: GetDataFail
});               

And one more question, how to change Content-Type to "application/json; charset=utf-8"? Because I see the Content-Type of pqgrid when use method "POST" is "application/x-www-form-urlencoded; charset=UTF-8".

paramvir

  • Administrator
  • Hero Member
  • *****
  • Posts: 5610
    • View Profile
Re: How to use dataModel.method = "POST"
« Reply #1 on: September 30, 2013, 07:54:38 pm »
Looks like it's routing to wrong url because of headers (content-type)

use http://paramquery.com/api#option-dataModel-beforeSend to set appropriate headers.

nghiemhd

  • Newbie
  • *
  • Posts: 16
    • View Profile
Re: How to use dataModel.method = "POST"
« Reply #2 on: October 01, 2013, 07:32:33 am »
Although I set contentType in beforeSend method, Content-Type of Request Headers is always "application/x-www-form-urlencoded; charset=UTF-8" and I can't call method GetMyClaimsWithSearch

dataModel.beforeSend = function (jqXHR, settings) {           
  settings.contentType = "application/json; charset=utf-8";
};

Do I set anything else?

paramvir

  • Administrator
  • Hero Member
  • *****
  • Posts: 5610
    • View Profile
Re: How to use dataModel.method = "POST"
« Reply #3 on: October 01, 2013, 05:19:50 pm »
Try this and let know whether this fixes your issue.

dataModel.beforeSend = function (jqXHR, settings) {           
  jqXHR.setRequestHeader("Content-Type", "application/json");   
};

« Last Edit: October 01, 2013, 06:26:54 pm by paramquery »

nghiemhd

  • Newbie
  • *
  • Posts: 16
    • View Profile
Re: How to use dataModel.method = "POST"
« Reply #4 on: October 02, 2013, 07:57:02 am »
Thank you very much.
Now I can post to method GetMyClaimsWithSearch by using
dataModel.beforeSend = function (jqXHR, settings) {           
  jqXHR.setRequestHeader("Content-Type", "application/json");   
};