1

msg以下のコードがnullでない場合にのみ成功を返すようにする方法を教えてください。

が定義されていないsuccess場合でも、関数を起動することがあります。msg.Text

$.ajax({
    async: true,
    url: url,
    type: "GET",
    data: params,
    success: function (msg) {
        if (msg.Text != undefined) {
            $('#mediumText').val(msg.Text).blur();
        } else {
            console.log("failed to load");
            return false;
        }
    }
});
4

4 に答える 4

1

長さとその空かどうかを確認できます。

$.ajax({
async: true,
url: url,
type: "GET",
data: params,
success: function (msg) 
{
      if (msg.Text != null && msg.Text.Length > 0) 
      {
           $('#mediumText').val(msg.Text).blur();
      }
      else
      {
          console.log("failed to load");
          return false;
      }
}
});
于 2013-03-28T11:58:30.563 に答える
1

応答の長さを確認できます。

$.ajax({
    async: true,
    url: url,
    type: "GET",
    data: params,
    success: function (msg) {
        if (msg.Text != undefined) {
            if (msg.Text.Length > 0) {
                $('#mediumText').val(msg.Text).blur();
            } else {
                console.log("failed to load");
                return false;
            }
        } else {
            console.log("failed to load");
            return false;
        }
    }
});
于 2013-03-28T11:47:52.997 に答える
1

の 値をチェックするだけです:

  if (msg.Text) {
     ...
  } else {
     ...
  }
于 2013-03-28T12:00:50.930 に答える
1

msg.Text が未定義の場合、それは呼び出しが失敗したことを意味しないため、success 関数を呼び出しても問題ありません。成功コールバック内の msg.Text を確認する必要があります。

于 2013-03-28T12:03:57.000 に答える