admin管理员组

文章数量:1278881

I'm making ajax POST request from javascript function:

function UpdateMetrics() {
   $.ajax({
              type: "POST",
              url: "MyHandler.ashx?Param1=value1",
              data: "{}",
              contentType: "text/json; charset=utf-8",
              dataType: "text",
              success: function (msg) {
                  var jsonUpdatedData = msg;
                  ...
              }
          });
}

From my handler, I'm sending json string with:

context.Response.write(json);

I think I'll get it in msg.

I also want to send other string (count). So I'm trying to use header info along with json data. So I added this line:

context.Response.Headers.Add("MaxCount",Convert.ToString(tempList.Count)); 

If this is right way to do it, how can I access it in my success function?

I'm making ajax POST request from javascript function:

function UpdateMetrics() {
   $.ajax({
              type: "POST",
              url: "MyHandler.ashx?Param1=value1",
              data: "{}",
              contentType: "text/json; charset=utf-8",
              dataType: "text",
              success: function (msg) {
                  var jsonUpdatedData = msg;
                  ...
              }
          });
}

From my handler, I'm sending json string with:

context.Response.write(json);

I think I'll get it in msg.

I also want to send other string (count). So I'm trying to use header info along with json data. So I added this line:

context.Response.Headers.Add("MaxCount",Convert.ToString(tempList.Count)); 

If this is right way to do it, how can I access it in my success function?

Share Improve this question asked Nov 26, 2012 at 4:53 mike44mike44 8125 gold badges15 silver badges38 bronze badges 3
  • I wouldn't suggest returning data in the response header. If you must return a count param, why not append it to your json? Or, if your json is returning an array of data, why not just get the array.length in javascript? – Steven Moseley Commented Nov 26, 2012 at 4:57
  • 2 You can just send it like, newdata={'data':json,'count':count}, send newdata instead of json. Sending via header is not remended. – specialscope Commented Nov 26, 2012 at 4:58
  • Actually count is some different count. It is not related with json string. – mike44 Commented Nov 26, 2012 at 4:58
Add a ment  | 

2 Answers 2

Reset to default 4

To access headers in your success function, add in 2 more arguments to your function, the status code and the jqXHR object, which you can read the documentation for at api.jquery..

So, your function should look like:

success: function (msg, status, jqXHR) {
    var jsonUpdatedData = msg;
    ...
}

However, as pointed out in ments, it's probably best not to use the header to send data. You should probably just include it in the json you send out.

You also need to tell jQuery to interpret the response as json by setting

dataType: "json"

Otherwise, it will just be returned to you as text.

Your requirement to get the header data in ajax post success can be achieved using getResponseHeader method please refer the below code snippet.

function UpdateMetrics() {
var callback =  $.ajax({
          type: "POST",
          url: "MyHandler.ashx?Param1=value1",
          data: "{}",
          contentType: "text/json; charset=utf-8",
          dataType: "text",
          success: function (msg) {

          var jsonUpdatedData = msg;
        var headerdata = callback.getResponseHeader("MaxCount"); 
// Where MaxCount is name provided in the header.
   ...
          }
      });
}

Thanks

本文标签: jqueryAccessing ajax POST response in javascriptStack Overflow