2011-09-13 69 views
0

我在我的页面上有2个Ajax函数。点击一个网格内的链接,点击一个按钮的链接。来自电网的那个完美地工作。但是,点击按钮的功能每次都会产生错误。AJAX函数webmethod不工作

有人可以告诉我在哪里我错了。我将不胜感激任何帮助。这是一个失败的:

function createSource() { 
     $.ajax({ 
      type: "POST", 
      url: "Test.aspx/createSource", 
      data: '{"schoolID":"' + $('#ddSchools').val() + '","vendor":"' + $('#txtVendor').val() + '","tSource":"' + $('#txtTSource').val()+ '"}', 
      contentType: "application/json", 
      dataType: "json", 
      success: function (msg) { 
       alert(msg.d); 
      }, 
      error: function (xhRequest, ErrorText, thrownError) { 
       alert(thrownError); 
      } 
     }); 
    } 

Webmethod将有更多的代码,但我似乎无法击中它。这里是webmethod:

[WebMethod] 
[System.Web.Script.Services.ScriptMethod(UseHttpGet = true)] 
public static string createSource(int schoolId, string vendor, string tSource) 
{ 
    try 
    { 
     string status = "This is a test string!"; 

     return status; 
    } 
    catch 
    { 
     throw new Exception("Could not create source code!"); 
    } 
} 

另外我怎么能得到导致此功能失败的确切错误?

预先感谢您的任何帮助!


好吧,所以我觉得问题在哪里,但我仍然没有解决方案。实际上代码没有任何问题。我将代码插入到document.ready中并正确触发。但是,无论何时点击按钮,它都不会正确启动。按钮也没什么特别。这是一个简单的输入按钮。

<input id="cmdSubmit_Create" value="Submit" type="submit" /> 

有什么想法吗?

仅供参考,我要把这对工作的document.ready代码:

$(document).ready(function() { 
    $.ajax({ 
      type: "POST", 
      url: "Test.aspx/createSource", 
      data: '{"schoolId":"2236","vendor":"test","tsource":"test1234"}', 
      contentType: "application/json", 
      dataType: "json", 
      success: function (msg) { 
       alert(msg.d); 
      }, 
      error: function (jqXHR, textStatus, errorThrown) { 
       alert("status: " + textStatus + " errorThrown: " + errorThrown); 
      }, 
      complete: function (jqXHR, textStatus) { 
       alert("status: " + textStatus); 
      } 
     }); 
}); 

[WebMethod] 
public static string helloWorld(string schoolId, string vendor,string tsource) 
{ 
    try 
    { 
     StringBuilder sb = new StringBuilder(); 
     sb.Append("Hello World! " + schoolId + '-' + vendor + '-' + tsource); 

     return sb.ToString(); 
    } 
    catch 
    { 
     throw new Exception("Could not create source code!"); 
    } 
} 

如果我尝试致电cmdSubmit_Create的点击它不工作相同的WebMethod:

 $('#cmdSubmit_Create').click(function() { 
     $.ajax({ 
      type: "POST", 
      url: "Test.aspx/helloWorld", 
      data: '{"schoolId":"2236","vendor":"test","tsource":"test1234"}', 
      contentType: "application/json", 
      dataType: "json", 
      success: function (msg) { 
       alert(msg.d); 
      }, 
      error: function (xhr, status, error) { 
       alert('error' + error) 
      } 
     }); 

回答

0

使用以下代码

$('#cmdSubmit_Create').click(function() { 
    $.ajax({ 
     type: "POST", 
     url: "Test.aspx/helloWorld", 
     data: '{"schoolId":"2236","vendor":"test","tsource":"test1234"}', 
     contentType: "application/json", 
     dataType: "json", 
     success: function (msg) { 
      alert(msg.d); 
     }, 
     error: function (xhr, status, error) { 
      alert('error' + error) 
     } 
    }); 
    return false; 
}); 

http://cmsnsoftware.blogspot.com/2011/01/how-to-call-csharp-function-in-ajax.html

+0

HA!我已经有一段时间了,只需要加入return false。感谢你的帮助,以及你的帮助3nigma。非常感谢! – Alex

0

你的服务期望GETUseHttpGet = true你在做什么POST试试而不是

type: "GET", 
+0

我道歉我删除的代码行[System.Web.Script.Services.ScriptMethod(UseHttpGet =真)。我把它放在那里试图使用GET但没有。 – Alex

+0

你看到萤火虫有什么错误吗? ajax调用是否被创建?你有答复吗? – Rafay

+0

我在萤火虫中看不到错误。 thrownerror回到未定义。甚至改变它不传递任何东西,只是尝试并返回字符串。没有!无论出于何种原因,它不会触及我的web方法。立即返回错误 – Alex