c# - 无法使用 json 和 ajax 调用从 webservice 方法获得响应

标签 c# jquery

我创建了一个 web 服务,它有两个方法,当我调用第一个方法时,它正在工作,但是当我调用 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;


    }
}

这里是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>`

最佳答案

要使 web 服务可由 JSON 使用,需要对 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;
    }

这是我自己的网络服务之一的提炼示例。 您会看到 Web 服务有一个 ResponseFormat 注释,数组有一个序列化。这应该适用于您。

请记住添加所需的用法和引用。

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

关于c# - 无法使用 json 和 ajax 调用从 webservice 方法获得响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26294780/

相关文章:

c# - 使用缓存依赖项对 MVC 操作方法进行单元测试?

jquery - 选择同一父级的所有先前子级

javascript - 无法使用 jQuery 解析 HTML

jquery - 简单的 JQuery 淡入淡出股票

javascript - 基于内部没有类的 jQuery 选择器

jquery - 在移动 View 中一次切换一个 div

c# - 我可以在 C# 中通过我的调制解调器接听电话吗

c# - 在 OnActionExecuting 中重定向不起作用

c# - ORMLite 下的 SQLite 不允许在事务完成后执行任何操作

c# - 如何在 Tridion 中为 C# TBB 添加第三方 dll?