2014-10-09 39 views
0

以下是html和代碼,但它不起作用。你們能幫助我解決這個問題嗎?Ajax asp.net引發內部服務器錯誤

function changeVideoSrc(title, videoLink) { 
     document.getElementById('current').innerHTML = 'Now Playing ... ' + title; 
     scroll(); 
     alert('{"videoSrc" : "' + videoLink + '",title" : "' + title + '"}'); 
     $.ajax({ 
      type: 'POST', 
      contentType: "application/json; charset=utf-8", 
      url: 'test.aspx/changeWebcamSource', 
      data: '{"videoSrc" : "' + videoLink + '"}', 
      dataType: "json", 
      async: false, 
      success: function (response) { 
       if (response.d.toString() == "success") 
        alert("Video Source Changed Successfully."); 
       else 
        alert("Fail to Update Video Source."); 
      }, 
      error: function (XMLHttpRequest, textStatus, errorThrown) { 
       if (textStatus == 'timeout') 
        alert('timeout'); 
       alert(errorThrown.toString()); 
      } 
     }); 
    } 

[WebMethod] 
[ScriptMethod(ResponseFormat = ResponseFormat.Json)] 
public string changeWebcamSource(String videoSrc, String title) 
{ 
    try 
    { 
     lblNowPlaying.Text = title; 
     mediaInitParams.Attributes.Add("value", 
      "selectedcaptionstream=textstream_eng,mediaurl=" + videoSrc); 
     return "success"; 
    } 
    catch (Exception ex) 
    { 
     return "failure"; 
    } 
} 

<object id="slObj" data="data:application/x-silverlight-2," type="application/x-silverlight-2" 
        width="100%" height="350"> 
        <param name="source" value="SmoothStreamingPlayer.xap" /> 
        <param name="onError" value="onSilverlightError" /> 
        <param name="background" value="white" /> 
        <param name="minRuntimeVersion" value="4.0.50401.0" /> 
        <param name="autoUpgrade" value="true" /> 
        <param name="InitParams" id="mediaInitParams" runat="server" value="" /> 
        <a href="http://go.microsoft.com/fwlink/?LinkID=149156&v=4.0.50401.0" style="text-decoration: none"> 
         <img src="http://go.microsoft.com/fwlink/?LinkId=161376" alt="Get Microsoft Silverlight" 
          style="border-style: none" /> 
        </a> 
       </object> 

回答

0

刪除引號,並嘗試

data: {videoSrc : videoLink,title: '' }, 
0

你必須的WebMethod兩個參數,但你只有經過一個從Ajax調用你缺少第二個參數的值,而是在報價中傳遞值的嘗試使用像這樣的字符串化來傳遞數據

data: JSON.stringify({'videoSrc':videoLink,'title':'Your title here'}), 
+0

不需要對json進行字符串化。是的,我忘了在那裏添加一個標題。錯誤背後的原因是我沒有添加jquery庫文件到頁面。謝謝。 – sudip 2014-10-09 11:30:02