2013-02-07 178 views
1

我必須在Web服務中工作,其中一個工作正常接收兩個參數,我可以執行它,但另一個也接收參數不起作用粘貼此函數和我的Ajax代碼,所以你可以幫助我看看發生了什麼。jquery ajax將參數傳遞給webservice

html 

<script type="text/javascript"> 
    function CallService() { 
     $.ajax({ 
      type: "POST", 
      url: "findMe.asmx/locateMe2", 
      crossDomain:true, 
      data: '{value1: ' + $("#txtValue1").val() + ', value2: ' + $("#txtValue2").val() + '}', 
      contentType: "application/json; charset=utf-8", 
      dataType: "json", 
      success: OnSuccess, 
      error: OnError 
     }); 

    } 

    function OnSuccess(data, status) { 
     $("#lblResult").html(data.d); 

    } 

    function OnError(request, status, error) { 
     $("#lblResult").html(request.statusText); 

    } 


</script>  

</head> 
<body> 
<form id="frmCoords" runat ="server" > 
    <div> 
<table> 
    <tbody> 
     <tr> 
      <th> 
       Value 1: 
      </th> 
      <td> 
       <asp:TextBox ID="txtValue1" runat="server" /> 
      </td> 
     </tr> 
     <tr> 
      <th> 
       Value 2: 
      </th> 
      <td> 
       <asp:TextBox ID="txtValue2" runat="server" /> 
      </td> 
     </tr> 
    </tbody> 
</table> 

<asp:Button ID="btnGo" Text="Go" OnClientClick="CallService(); return false;" runat="server" /> 

<asp:Label ID="lblResult" Text="&nbsp;" Width="100%" runat="server" ForeColor="black" /> 
    </div> 

</form> 


--webservice 
[WebService(Namespace = "http://tempuri.org/")] 
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] 
[System.ComponentModel.ToolboxItem(false)] 
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
[System.Web.Script.Services.ScriptService] 


    [WebMethod] 
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)] 

    public int Add(int value1, int value2) 
    { 
     return value1 + value2; 
    } 

    public string locateMe2(Double value1, Double value2) 
    { 

     FormClosingEventArgs ee = new FormClosingEventArgs(CloseReason.UserClosing, false); 
     DialogResult dlgResult = MessageBox.Show("", "Cadena", MessageBoxButtons.OK, MessageBoxIcon.Information); 

     SqlConnection Conn = new SqlConnection(); 
     Conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["Audi.Properties.Settings.ConexionSql"].ConnectionString); 
     string procedure = "usp_PointInPolygon"; 

     SqlCommand cmd = new SqlCommand(procedure, Conn); 
     SqlDataReader reader; 


     //cmd.CommandText = "usp_PointInPolygon"; 
     cmd.CommandType = CommandType.StoredProcedure; 
     //cmd.Parameters.Clear(); 
     SqlParameter param1; 
     SqlParameter param2; 
     param1 = cmd.Parameters.Add("@lat", SqlDbType.Float,14); 
     param2 = cmd.Parameters.Add("@lng", SqlDbType.Float,14); 

     param1.Value = value1; 
     param2.Value = value2; 

     Conn.Open(); 

     reader = cmd.ExecuteReader(); 
     string column = ""; 
     while (reader.Read()) 
     { 
      column = reader["county"].ToString(); 
      //int columnValue = Convert.ToInt32(reader["ColumnName"]); 
     } 
     Conn.Close(); 

     return column; 

    } 

功能添加做工精細,它接收兩個int值,誰也接收到的緯度和經度,並彩車不起作用值的功能locateMe2,你看到什麼了嗎?

the locateMe2 function will return just a string 
+0

你看到什麼回來的瀏覽器/ Firebug的控制檯在運行時? –

+0

爲什麼crossDomain設置爲true?您是否在使用CORS進行同域申請? –

+0

我猜想第二個方法'locateMe2'沒有接收到你缺少屬性'[WebMethod]'和 '[ScriptMethod(ResponseFormat = ResponseFormat.Json)]''的參數。 – dee

回答

-1
var params = {value1: $("#txtValue1").val(), value2: $("#txtValue2").val()}; 
function CallService() { 
    $.ajax({ 
     type: "POST", 
     url: "findMe.asmx/locateMe2", 
     crossDomain:true, 
     data: JSON.stringify(params), 
     contentType: "application/json; charset=utf-8", 
     dataType: "json", 
     success: OnSuccess, 
     error: OnError 
    }); 
} 

值可以直接被置爲字符串「數據」的設置把設定值以外的AJAX方法有助於監控從變量或元素中獲得價值,當你寫的東西特別,和「字符串」方法有助於將值放在字符串形狀中,因爲它是必需的。

+0

考慮添加一些細節或解釋爲什麼這個片段解決了這個問題。 –