0

人気のあるファイル jquery.form.js を含めます。私は次のものを持っています:

 var options = {
    beforeSubmit: beforeSubmit,  // pre-submit callback 
    success: afterSubmit  // post-submit callback 
  };

 $('#myForm').submit(function (e) {
     $(this).ajaxSubmit(options);
     return false;
  });


 function afterSubmit(responseText, statusText, xhr, $form) {
   // i want to check for an error
}

このアクションを次のように呼びます。

    [HttpPost] 
    public string UploadDocument(DocumentModel model)
    {
        if (noerror)
        return "ok";
        else
         return "the error";
    }

パラメータ「responseText、statusText、xhr、$form」のどこかに戻り文字列が格納されます。

「afterSubmit」JavaScript 関数が呼び出されたときに結果を確認できるように、戻り文字列はどこに保存されますか、またはどのように保存すればよいですか? ありがとう

4

1 に答える 1

3

コントローラー アクションから文字列を返さないでください。ASP.NET MVC コントローラー アクションでは、ActionResults を返す必要があります。

[HttpPost] 
public string UploadDocument(DocumentModel model)
{
    if (noerror)
        return Content("ok");
    else
        return Content("the error");
}

結果は変数に格納されresponseTextます。

明らかに、JavaScript でのテストはif (responseText == 'ok')絶対に恐ろしいもののように思えます。このため、JSON があります。

[HttpPost] 
public string UploadDocument(DocumentModel model)
{
    if (noerror)
        return Json(new { success = true });
    else
        return Json(new { success = false });
}

JavaScript で、基になる型 (この場合はブール値) を直接操作できるようにします。

function afterSubmit(response, statusText, xhr, $form) {
    if (response.success) {
        alert('super! we succeeded');
    } else {
        alert('Oh snap!');
    }
}
于 2012-06-14T15:54:46.187 に答える