如何从webmethod捕获json中的返回值

本文关键字:返回值 json 捕获 webmethod | 更新日期: 2023-09-27 18:05:49

使用JSON的Net和Webmethods

我的webmethod返回值是

    return ResultValue;
      // which gives 1 as Return Value on SUcessfull insertion

如果返回值为1,我希望它在JQuery中显示为success

,我应该如何捕获返回值是在我的webmethod??

如何从webmethod捕获json中的返回值

在您的ajax代码中试试:

$.ajax({
    type: "POST",
    success: function(data) {  
      //data=ResultValue
      if(data == 1)
       { // Success Stuff }else {//do stuff}
    },
    error: function(XMLHttpRequest, textStatus, errorThrown) {
        alert("error: " + XMLHttpRequest.responseText);
    },
    dataType: 'json'
});

$.ajax({
  url: 'mypage.html',
  success: function(data){
    if(data ==1)
       alert('success');
    else
       alert('failure');
  },
  error: function(){
    alert('failure');
  }
});
http://api.jquery.com/jQuery.ajax/

或者你在你的webmethod中为你的Response对象设置状态码。像这样:

Response.Clear();
Response.StatusCode = 500; // or whatever code is appropriate
Response.End;