2013-08-21 46 views
1

今天刚开始学习jQuery ajax,跟着教程说了什么,但没有奏效。我的jQuery ajax不起作用

HelloWorld是方法名称,但它似乎不被识别为方法名称,而是基于错误消息被识别为页面名称。

jQuery的

$(document).ready(function() { 
    //alert("hello world"); 
    $('.ordernumber').on('click', function() { 
     var orderNum = $(this).text(); 
     $.ajax({ 
      type: "POST", 
      url: "./OrderDetail.asmx/HelloWorld", 
      data: "{}", 
      contentType: "application/json; charset=utf-8", 
      dataType: "json", 
      success: function (msg) { 
       alert(msg); 
       // Do interesting things here. 
      } 
     }); 
     //alert($(this).text()); 

    }); 
}); 

OrderDetail.asmx.vb

Imports System 
Imports System.Web.Services 

Public Class OrderDetail 
Inherits System.Web.Services.WebService 

<WebMethod()> _ 
Public Function HelloWorld() As String 
    Return "Hello World" 
End Function 

End Class 

错误消息:

POST http://localhost:64616/OrderDetail.asmx/HelloWorld 500 (Internal Server Error) 
+1

'内部服务器Error'不是jQuery的故障。 – gdoron

+0

嗨,请尝试在IE中运行它,从那里开发者工具,你将能够知道什么是错误。如果它的内部服务器错误。然后可能会在您正在访问的页面中出现编码错误(OrderDetail)jQuery运行得非常好! –

+0

@AzzaalAhmadZeeshan IE是** NOT **调试ajax – Eonasdan

回答

3

我认为你需要添加<System.Web.Script.Services.ScriptService()>你的课;

<System.Web.Script.Services.ScriptService()> _ 
Public Class OrderDetails 
    Inherits System.Web.Services.WebService 

    '' rest of your code 

End Class 

也要返回JSON,你需要装饰你的方法;

<ScriptMethod(ResponseFormat:=ResponseFormat.Json)> _ 

更新

当创建一个新的ASMX Web服务,默认代码状态;

' To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
' <System.Web.Script.Services.ScriptService()> _ 
+0

为什么?这是一个VB的东西? – gdoron

+0

这是一个.Net的东西。在C#中它将是相同的,但[[System.Web.Script.Services.ScriptService]' –

+0

@TimBJames我现在添加它们。但它返回[object Object]而不是“Hello World” – Quentin

1

你期待一个JSON回来,但一个ASMX web服务返回的XML,而不是你需要添加

<WebMethod()> _ 
<ScriptMethod(ResponseFormat:=ResponseFormat.Json)>_ 
Public Function HelloWorld() As String 
Return "Hello World" 
End Function 

The link for more explanaiton