2014-10-09 37 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