簡體   English   中英

如何從Web方法捕獲json中的返回值?

[英]How to catch the return value in json from a webmethod?

使用JSON的Net和Web方法

我的webmethod返回值給出為

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

如果返回值為1,我希望它在我的JQuery中顯示為成功

為此,我該如何捕捉我的web方法中的返回值?

在您的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/

或者您可以在網絡方法中為Response對象設置狀態代碼。 像這樣:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM