无法从jQuery ajax访问Web服务方法

时间:2010-05-17 07:19:18

标签: web-services jquery

我正在使用jQuery ajax来调用Web服务方法但是没有做并生成错误..

代码在这里用于asp页面中的jQuery ajax

var indexNo = 13; //pass the value

    $(document).ready(function() {
        $("#a1").click(function() {
         $.ajax({
                type: "POST",
                url: "myWebService.asmx/GetNewDownline",
                data: "{'indexNo':user_id}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function(msg) {
                $("#divResult").text(msg.d);
                }
        });
    });
    });

这是网络服务方法

using System;
using System.Collections;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
using System.Data;
using System.Web.Script.Serialization;
using TC.MLM.DAL;
using TC.MLM.BLL.AS;

/// <summary>
/// Summary description for myWebService
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[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 myWebService : System.Web.Services.WebService
{
    public myWebService()
    {
        //Uncomment the following line if using designed components 
        //InitializeComponent(); 
    }

    [WebMethod]
    public string HelloWorld()
    {
        return "Hello World";
    }

    [WebMethod]
    public string GetNewDownline(string indexNo)
    {
        IndexDetails indexDtls = new IndexDetails();
        indexDtls.IndexNo = "13";
        DataSet ds = new DataSet();
        ds = TC.MLM.BLL.AS.Index.getIndexDownLineByIndex(indexDtls);
        indexNoDownline[] newDownline = new indexNoDownline[ds.Tables[0].Rows.Count];

        for (int count = 0; count <= ds.Tables[0].Rows.Count - 1; count++)
        {
            newDownline[count] = new indexNoDownline();
            newDownline[count].adjustedid = ds.Tables[0].Rows[count]["AdjustedID"].ToString();
            newDownline[count].name = ds.Tables[0].Rows[count]["name"].ToString();
            newDownline[count].structPostion = ds.Tables[0].Rows[count]["Struct_Position"].ToString();
            newDownline[count].indexNo = ds.Tables[0].Rows[count]["IndexNo"].ToString();
            newDownline[count].promoterId = ds.Tables[0].Rows[count]["PromotorID"].ToString();
            newDownline[count].formNo = ds.Tables[0].Rows[count]["FormNo"].ToString();
        }

        JavaScriptSerializer serializer = new JavaScriptSerializer();
        JavaScriptSerializer js = new JavaScriptSerializer();
        string resultedDownLine = js.Serialize(newDownline);

        return resultedDownLine;
    }

    public class indexNoDownline
    {
        public string adjustedid;
        public string name;
        public string indexNo;
        public string structPostion;
        public string promoterId;
        public string formNo;
    }
}

请帮帮我一些事。

2 个答案:

答案 0 :(得分:0)

输入的JSON数据存在问题。您应该尝试使用内置的JSON类,而不是手动序列化。实施例

$.ajax({
   ...
   data: JSON.stringify({ indexNo: user_id }),
   ...
 });

这可以解决您的问题。

答案 1 :(得分:0)

您应该更改在服务器中实现序列化的方法。只需使用ResponseFormat = ResponseFormat.Json添加ScriptMethod属性即可。对于附加(可选)属性UseHttpGet = true

,也可以使用HTTP GET
[WebMethod]
[ScriptMethod (UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
public string GetNewDownline(string indexNo)

如果您使用SqlConnectionSqlCommandSqlDataReader类或DbDataReader的其他版本取决于您的数据库源({{1>),也可以改进数据库访问}},OleDbDataReaderOracleDataReaderOdbcDataReader)。如果您更喜欢使用强类型数据,那么Visual Studio生成的DataTableReader将是SqlDataAdapter的更好版本。要执行此操作,只需在项目中添加一个新项目,选择“数据”/“数据集”,然后添加查询或DataSet

如果您决定使用TableAdapter HTTP,则不应对GET文件进行相应修改。

web.config

关于<configuration> <!-- ... --> <system.web> <webServices> <protocols> <add name="HttpGet"/> </protocols> </webServices> <!-- ... --> </system.web> </configuration> 的使用我和“egyedg”有相同的意见。

我建议您查看以下链接:

Can I return JSON from an .asmx Web Service if the ContentType is not JSON?

How do I build a JSON object to send to an AJAX WebService?

JQuery ajax call to httpget webmethod (c#) not working