3

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?

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? 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 recommended. Commented Nov 26, 2012 at 4:58
  • Actually count is some different count. It is not related with json string. Commented Nov 26, 2012 at 4:58

2 Answers 2

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.com.

So, your function should look like:

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

However, as pointed out in comments, 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.

Sign up to request clarification or add additional context in comments.

1 Comment

Setting dataType isn't necessary if the response from the server has a valid Content-type. jQuery will use this to attempt to convert the response's value to the type specified. It isn't always just text
0

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

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.