Web服务不将数据返回到AJAX调用 [英] Web service not returning data to AJAX call

查看:92
本文介绍了Web服务不将数据返回到AJAX调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的任务是建立一个HTML5网站。该网站需要连接到MS SQL数据库并将数据从其传递到网站供用户查看。我必须构建一个连接到SQL数据库的Web服务。我打算将数据传递到网页以填充列表框并使用列表框选择的方法使用数据填充图表。我可以从IIS服务器和WSDL网站上看到数据。如果我将它指向Web地址,我可以使用SOAPUI查看数据。但是,当我在Javascript中运行AJAX脚本时,它不会返回数据。我很困惑。



我已经附加了Webservice代码以及我在Javascript中使用的代码来调用Web服务。



我尝试过:



I have been tasked with building an HTML5 website. The website will need to connect to an MS SQL database and pass data from it to the Website for the user to review. I have to build a Web Service which connects to the SQL database. The Methods I had intended to pass data to the Web page to populate a List box and to use the listbox selection populate a graph with the data. I can see the data from the IIS server and the WSDL website. I can see the data using SOAPUI if I point it at the Web address. However, when I run the AJAX script within Javascript it is not returning the data. I am puzzled.

I have attached the Webservice Code as well as the code I am using in the Javascript to call the web service.

What I have tried:

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





[WebService(Namespace = "http://tempuri.org/",
    Description = "Database Connection Service",
    Name = "UnivariateWebService")]
//[System.ComponentModel.ToolboxItem(false)]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// 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 Service : System.Web.Services.WebService
{
    public Service () {

        //Uncomment the following line if using designed components 
        //InitializeComponent(); 
    }

    [WebMethod]
    public DataSet GetBatchData()
    {

        //Modify this connection string to use your SQL Server and log on information.
        var con = new SqlConnection("server=DEVELOPMENT-SER;uid=******;pwd=******;database=UnivariateRuns");

        //Open the Customers table to serve as the parent table.
        var daRunSet = new SqlDataAdapter("Select * From dbo.Products Order By product desc",
            con);

        //Create a client-side DataSet to hold the Customers and Orders tables.
        var ds = new DataSet();

        //Explicitly open the connection to allow explicit closing.
        con.Open();

        //Fill the DataSet with the Customers table and the Orders table.
        daRunSet.Fill(ds, "Batches");
        //string myJsonString = (new JavaScriptSerializer()).Serialize(ds);     // might need to phase the dataset as a string
        //Explicitly close the connection - do not wait for garbage collection.
        con.Close();

        //Return the DataSet to the client.
        return ds;
    }

    [WebMethod]
    public DataSet GetSampleData(string product)
    {
       
        //Modify this connection string to use your SQL Server and log on information.
        var con = new SqlConnection("server=DEVELOPMENT-SER;uid=******;pwd=******;database=UnivariateRuns");

        //Open the Customers table to serve as the parent table.
        var daRunSet = new SqlDataAdapter("Select * From dbo.RunSetData where product like '%" + product + "%' Order by product desc", con);

        //Create a client-side DataSet to hold the Customers and Orders tables.
        var ds=new DataSet();

        //Explicitly open the connection to allow explicit closing.
        con.Open();

        //Fill the DataSet with the Customers table and the Orders table.
        daRunSet.Fill(ds, "RunSet");
        //daOrders.Fill(ds, "Orders");

        //Explicitly close the connection - do not wait for garbage collection.
        con.Close();

        //Return the DataSet to the client.
        return ds;
    }
    
}



Javascript Function:

function DataConnect() {
   		$.ajax({
   			type: 'GET',
   			url: 'http://192.168.1.103/Web%20Service/Service.asmx/GetBatchData',
   			dataType: 'json',
   			contentType: 'application/json;charset=utf-8',
   			success: function(responce) {
   				var names = response.d;
                 alert(names);
            },
   			failure: function(error) {
   				 alert(response.d); 
   			}
   		});
   };

// How do I get the data passed to the Plot Array from the AJAX call?

推荐答案

.ajax({
type:'GET',
url:'http://192.168.1.103/Web%20Service/Service.asmx/GetBatchData',
dataType:'json',
contentType:'application / json; charset = utf-8',
success:function(responce){
var names = response.d;
alert(names);
},
失败:函数(错误){
alert(response.d);
}
});
};

//如何从AJAX调用中获取传递给Plot Array的数据?
.ajax({ type: 'GET', url: 'http://192.168.1.103/Web%20Service/Service.asmx/GetBatchData', dataType: 'json', contentType: 'application/json;charset=utf-8', success: function(responce) { var names = response.d; alert(names); }, failure: function(error) { alert(response.d); } }); }; // How do I get the data passed to the Plot Array from the AJAX call?


using System.Data;
using System.Web.Services;
using System.Data.SqlClient;
using Newtonsoft.Json;


[WebMethod]
    public string GetSampleData(string product)
    {
       
        //Modify this connection string to use your SQL Server and log on information.
        var con = new SqlConnection("server=DEVELOPMENT-SER;uid=*****;pwd=******;database=UnivariateRuns");

        //Open the Customers table to serve as the parent table.
        var daRunSet = new SqlDataAdapter("Select * From dbo.RunSetData where product like @product Order by product desc", con);
        daRunSet.SelectCommand.Parameters.AddWithValue("@product", "%" + product + "%");

        //Create a client-side DataSet to hold the Customers and Orders tables.
        var ds=new DataSet();

        

        //Explicitly open the connection to allow explicit closing.
        con.Open();

        //Fill the DataSet with the Customers table and the Orders table.
        daRunSet.Fill(ds, "RunSet");
        DataTable table = new DataTable();
        DataColumn idColumn = new DataColumn("id", typeof(int));
        idColumn.AutoIncrement = true;

        DataColumn itemColumn = new DataColumn("item");
        table.Columns.Add(idColumn);
        table.Columns.Add(itemColumn);
        ds.Tables.Add(table);

        for (int i = 0; i < 2; i++)
        {
            DataRow newRow = table.NewRow();
            newRow["item"] = "item " + i;
            table.Rows.Add(newRow);
        }

        ds.AcceptChanges();
        string json = JsonConvert.SerializeObject(ds, Formatting.Indented);
        //daOrders.Fill(ds, "Orders");

        //Explicitly close the connection - do not wait for garbage collection.
        con.Close();
        
        //Return the DataSet to the client.
        return json;
    }





这是代码导致内存不足异常,是否有更好的方法来安排它?



This is code is resulting in an Out of Memory Exception, is there a better way to arrange it?


返回太大的数据集


这篇关于Web服务不将数据返回到AJAX调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆