2014-10-10 146 views
3

我創建了一個webservice,它有兩個方法,當我調用第一個方法時它正在工作,但是當我調用getpostings方法時無法獲得響應,但能夠調用的方法無法從使用json和ajax的webservice方法獲得響應

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.Services; 
using System.Data; 
using System.Data.SqlClient; 
using System.Web.Script.Services; 
using System.Web.Script.Serialization; 


    namespace SimpleService 
{ 
public class Errors 
{ 
    public int id { get; set; } 
    public int data { get; set; } 
} 
/// <summary> 
/// Summary description for Service1 
/// </summary> 
[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] 
public class Service1 : System.Web.Services.WebService 
{ 
    [WebMethod] 
    public string GetCurrentTime(string name) 
    { 
     return "Hello " + name + Environment.NewLine + "The Current Time is: " 
      + DateTime.Now.ToString(); 
    } 

    [WebMethod] 
    //[ScriptMethod(UseHttpGet=true)] 
    [ScriptMethod(ResponseFormat = ResponseFormat.Json,UseHttpGet = true)] 
    public List<Errors> GetPostings() 
    { 

     string connetionString = null; 
     SqlConnection cnn; 
     connetionString = "Data Source=mycomputer.\\SCOM;Initial Catalog=Portal;Integrated Security=True"; 
     cnn = new SqlConnection(connetionString); 

     const string SQL = "select id,openemr from openemrdata"; 
     SqlCommand myCommand = new SqlCommand(SQL, cnn); 
     cnn.Open(); 
     myCommand.ExecuteNonQuery(); 
     // myConnection.Close(); 

     SqlDataAdapter dataAdapter = new SqlDataAdapter(); 
     dataAdapter.SelectCommand = myCommand; 

     DataSet DSet = new DataSet(); 
     dataAdapter.Fill(DSet); 
     JavaScriptSerializer js = new JavaScriptSerializer(); 
     string strJSON = js.Serialize(JaggedArray); 

     List<Errors> errors = new List<Errors>(); 
     foreach (DataRow row in DSet.Tables[0].Rows) 
     { 
      Errors jp = new Errors(); 

      jp.id = (int)row["id"]; 
      jp.data = (int)row["openemr"]; 
      //jp.Id = (int)row["Id"]; 
      //jp.JobPost = row["JobPosting"].ToString(); 
      //jp.Applicant = row["Applicant"].ToString(); 
      //jp.Type = row["Type"].ToString(); 
      errors.Add(jp); 
     } 


     return errors; 


    } 
} 

}

這裏是Java腳本代碼

`<script type = "text/javascript"> 
    function ShowCurrentTime() { 
     alert(1); 
     $.ajax({ 
      type: "GET", 
      url: "~/Service1.asmx/GetPostings", 
      data: '', 
     contentType: "application/json; charset=utf-8", 
     dataType: "json", 
     success: OnSuccess, 
     failure: function (response) { 
      alert(1); 
      alert(response.d); 
     } 
    }); 
} 
function OnSuccess(response) { 

    alert(1); 
     var myData = response.d; 
     var data1; 
     var data2; 
     alert(suresh); 
     alert(myData.length + " hellos"); 
     for (var i = 0; i < myData.length; i++) { 

      $("#chart").append(myData[i].id + " " + myData[i].data); 
      var list = myData; 
     } 
     for(var i=0;i<list.length;i++) 
     { 
      $("#chart").append(list[i].openemr); 
     }; 
} 
</script>` 
+0

它是否適用於SoapUI?或其他類似的。 – 2014-10-10 08:19:01

+0

你在用什麼? ASP.NET或ASP.MVC? – 2014-10-10 08:19:19

+0

不,它不起作用,是否有任何要添加到web.config文件中? – chinna2580 2014-10-10 08:20:14

回答

0

使用POST而不是得到任何其他具有以下屬性

裝飾你的webmethode
[WebMethod] 
[ScriptMethod(UseHttpGet = true)] 

還添加在web.config中以下

<webServices> 
    <protocols> 
    <add name="HttpGet"/> 
    <add name="HttpPost"/> 
    </protocols> 
</webServices> 
+0

電話正在作出,但沒有作出迴應,無法接收響應 – chinna2580 2014-10-10 08:33:52

+0

添加另一個屬性[ScriptMethod(ResponseFormat = ResponseFormat.Json)]使用此。如果請求的響應格式是json – 2014-10-10 08:34:40

+0

什麼是工作函數?失敗的功能或成功 – 2014-10-10 08:36:25

1

要通過JSON webservice的耗材有兩件事情是需要做的WebService 。

[WebMethod] 
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)] 
    public string GetPage(string pagenam) 
    { 
     string[] JaggedArray = arr; 
     JavaScriptSerializer js = new JavaScriptSerializer(); 
     string strJSON = js.Serialize(JaggedArray); 
     return strJSON; 
    } 

這是來自我自己的web服務之一的蒸餾示例。 您會看到webservice有一個ResponseFormat註釋,並且存在對該數組的序列化。這應該與你的工作。

Remmeber添加reqired使用和引用。

using System.Web.Script.Services; 
using System.Web.Script.Serialization;