2011-12-12 3 views
0
에서 다른 자바 스크립트 객체를 반환

내 컨트롤러 액션 : 나는 자바 스크립트를 반환하고 어떻게 든 반환하고 그 결과에 기반을 둔 있었는지의 클라이언트에 통지 할 다른 시나리오를 기반으로컨트롤러

[HttpPost] 
    public ActionResult AddPointAndCopyOtherSongToPlaylist(int id) 
    { 
     if (CheckIfAddPointToSelf(User.Identity.Name, id)) 
     { 
      var song = repository.GetSong(id); 
      foreach (var item in song.Points) 
      { 
       if (User.Identity.Name == item.UsernameGavePoint) 
       { 
        var data1 = 1; 


        return Json(new {data1}, JsonRequestBehavior.AllowGet); 
       } 

      } 
      var originalSong = repository.GetSong(id); 
      var newSong = new Song(); 
      newSong.UserName = User.Identity.Name; 
      newSong.Title = originalSong.Title; 
      newSong.YoutubeLink = originalSong.YoutubeLink; 
      newSong.GenreId = 38; 
      newSong.Date = DateTime.Now; 

      repository.AddSong(newSong); 

      var point = new Point(); 
      point.UsernameGotPoint = originalSong.UserName; 
      point.UsernameGavePoint = User.Identity.Name; 
      point.Date = DateTime.Now; 
      point.Score = 1; 
      point.OtherSongId = id; 
      repository.AddPoint(point); 
      repository.Save(); 

      int data = 2; 
      //process here 
      return Json(new { data }, JsonRequestBehavior.AllowGet); 
     } 
     else 
     { 
      return null; 

     } 
    } 

이 성공 부분에 뭔가를 할 내 아약스 호출의 :

$.ajax({ 
      beforeSend: function() { ShowAjaxLoader(); }, 
      url: "/Home/AddPointAndCopyOtherSongToPlaylist/", 
      type: "POST", 
      data: { id: songId }, 
      success: function (data,one) { 

       if (data && !one) { 
        HideAjaxLoader(), ShowMsg("Song Added Successfully"); 
       } 
       else if(!data) { 
        HideAjaxLoader(), ShowMsg("you cannot add your own songs"); 
       } 
       else if (data && one) { 
        HideAjaxLoader(), ShowMsg("You cannot add the same song twice"); 
       } 
      }, 
      error: function() { HideAjaxLoader(), ShowMsg("Song could not be added, please try again") } 
     }); 
    }); 

나는 많은 다른 변화를 시도하지만 난 그 속성이 존재하거나 그런 soemthing 있는지 확인하는 data.property1 반환처럼 뭔가가 필요하고 클라이언트에서 생각 .. 도와주세요

답변

1

개체 내에서 상태 코드를 반환해야합니다.

return Json(new { data1 = "Some Other Data", status = 1}); 

그런 다음 성공 처리기에서 data.status를 확인하십시오.

if (data.status === 1) { 
    alert(data.data1); 
} 
+0

그래서 상태 = 1 또는 상태 = 2와 같은 작업을 수행 할 수 있으며 상태에 대한 chceck은 어떤 숫자입니까? –

+0

확실한 것. 321 – BNL

+0

끝내 주셔서 감사합니다! ~ –

관련 문제