简体   繁体   English

无法从使用json和ajax调用的webservice方法获得响应

[英]unable to get response from a webservice method using json and ajax call

I have created a webservice and it has two methods and when i call the first method it is working but when i call the getpostings method am unable to get the response but able to call the method 我已经创建了一个webservice,它有两个方法,当我调用第一个方法时它正在工作但是当我调用getpostings方法时我无法得到响应但是能够调用方法

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;


    }
}

} }

here is java script code 这是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>`

To make a webservice consumable by JSON there is 2 things that needs to be done to the webservice. 要通过JSON创建Web服务消耗品,需要对Web服务执行两项操作。

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

This is a distilled example from one of my own webservices. 这是我自己的一个Web服务的一个提炼示例。 You see that there is a ResponseFormat Annotation to the webservice and a serialization to the array. 您会看到Web服务有一个ResponseFormat Annotation和一个到该数组的序列化。 This should work with yours to. 这应该与你合作。

Remmeber to add the reqired usings and references. Remmeber添加所需的使用和参考。

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

Use POST instead of GET else decorate your webmethode with following attributes 使用POST代替GET否则使用以下属性装饰您的webmethode

[WebMethod]
[ScriptMethod(UseHttpGet = true)]

also add following in web.config 还在web.config中添加以下内容

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM