2012-06-07 49 views
0

我嘗試開發一個簡單的登錄表單,其中將按鈕張貼表單數據提交給asmx文件中的方法。
問題是,我嘗試從窗體標籤中發佈信息時出現錯誤。
發佈外部表單標籤正常工作。
我提供我的問題的一個簡單的例子:使用jQuery將表單數據張貼到ASP.NET ASMX Web服務

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server"> 
<title></title> 
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script> 

<script type="text/javascript"> 

$(document).ready(function() { 

    $("#submitOutsideForm").click(function() { 
     AjaxPost(); 
    }); 

    $("#submitInsideForm").click(function() { 
     AjaxPost(); 
    }); 
}); 

function AjaxPost() { 
    try { 
     $.ajax({ 
      type: "POST", 
      url: "dummyWS.asmx/HelloToYou", 
      data: "{'name': '" + $('#name').val() + "'}", 
      contentType: "application/json; charset=utf-8", 
      dataType: "json", 
      success: function (msg) { 
       AjaxSucceeded(msg); 
      }, 
      error: AjaxFailed 
     }); 
    } 
    catch (e) { 
     alert("Exception in ajaxCall2Func: " + e.name + ".\n Error messege: " + e.message); 
    } 
} 

function AjaxSucceeded(result) { 
    alert(result.d); 
} 

function AjaxFailed(result) { 
    alert(result.status + ' ' + result.statusText); 
} 
</script> 
</head> 
<body> 

Enter your name: 
<input id="name" /> 

<form id="form1" runat="server" style="border:1px solid green;"> 
<div> 
    <label for="email">some other form controls</label> 
</div> 
<input id="submitInsideForm" type="submit" value="Submit Inside Form" /> 
</form> 
<input id="submitOutsideForm" type="submit" value="Submit Outside Form" /> 
</body> 
</html> 

在服務器端我有這個簡單的假的WebService

<%@ WebService Language="C#" CodeBehind="~/App_Code/dummyWS.cs" Class="dummyWS" %> 

...和後面的代碼:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.Services; 

[WebService(Namespace = "http://tempuri.org/")] 
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] 
[System.Web.Script.Services.ScriptService] 
public class dummyWS : System.Web.Services.WebService { 

    [WebMethod()] 
    public string HelloToYou(string name) 
    { 
     return "Hello " + name; 
    } 
} 
+0

你什麼錯誤? – Ahmad

+0

result.status = 0 result.statusText在那裏沒什麼幫助。 – user1441800

回答

0

嘗試設置你的HelloToYou方法是靜態的。

[WebMethod()] 
public static string HelloToYou(string name) 
{ 
    return "Hello " + name; 
}