本文介绍了从jQuery的调用ASMX的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从jQuery的调用ASMX方法没有成功。以下是我的code,我不明白我错过了什么。

文件Something.js,

 函数setQuestion(){
    $阿贾克斯({
        键入:POST,
        数据:{},
        数据类型:JSON
        网址:HTTP://localhost/BoATransformation/Survey.asmx/GetSurvey
        的contentType:应用/ JSON的;字符集= UTF-8,
        成功:的onSuccess
    });
}功能的onSuccess(MSG){
    $(#questionCxt)追加(MSG)。
}

文件SomethingElse.cs,

  [WebService的空间(namespace =htt​​p://tempuri.org/)]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)
[System.Web.Script.Services.ScriptService]
公共类调查:System.Web.Services.WebService {    公众调查(){
    }    [的WebMethod]
    [ScriptMethod(UseHttpGet =真)]
    公共字符串GetSurvey(){
        返回问题:谁是史努比?
    }
}


解决方案

这突出的一件事是你 UseHttpGet = TRUE 但在你的jQuery code你使用POST。

另外这里是我创建调用一个ASMX页测试页。

  [的WebMethod]
公共目录[] GetCatalog()
{
    目录[]目录=新目录[1];
    目录猫=新目录();
    cat.Author =吉姆;
    cat.BookName =他的著作;
    catalog.SetValue(猫,0);
    返回目录;
}<脚本类型=文/ JavaScript的>
    $(文件)。就绪(函数(){
    $阿贾克斯({
            键入:POST,
            网址:default.asmx / GetCatalog
            缓存:假的,
            的contentType:应用/ JSON的;字符集= UTF-8,
            数据:{},
            数据类型:JSON
            成功:handleHtml,
            错误:ajaxFailed
        });
    });    功能handleHtml(数据,状态){
        对(在data.d VAR计数){
            警报(data.d [计数] .Author);
            警报(data.d [计数] .BookName);
        }
    }    功能ajaxFailed(xmlRequest){
        警报(xmlRequest.status +\\ n \\ r'+
              xmlRequest.statusText +\\ n \\ r'+
              xmlRequest.responseText);
    }
< / SCRIPT>

I am trying to call an ASMX method from jQuery without success. Following is my code, and I don't understand what I am missing.

File Something.js,

function setQuestion() {
    $.ajax({
        type: "POST",
        data: "{}",
        dataType: "json",
        url: "http: //localhost/BoATransformation/Survey.asmx/GetSurvey",
        contentType: "application/json; charset=utf-8",
        success: onSuccess
    });
}

function onSuccess(msg) {
    $("#questionCxt").append(msg);
}

File SomethingElse.cs,

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService]
public class Survey : System.Web.Services.WebService {

    public Survey () {
    }

    [WebMethod]
    [ScriptMethod(UseHttpGet = true)]
    public string GetSurvey() {
        return "Question: Who is Snoopy?";
    }
}
解决方案

One thing that stands out is you have UseHttpGet=true but in your jQuery code you are using POST.

Also here is a test page I created calling an ASMX page.

[WebMethod]
public Catalog[] GetCatalog()
{
    Catalog[] catalog = new Catalog[1];
    Catalog cat = new Catalog();
    cat.Author = "Jim";
    cat.BookName ="His Book";
    catalog.SetValue(cat, 0);
    return catalog;
}

<script type="text/javascript">
    $(document).ready(function() {
    $.ajax({
            type: "POST",
            url: "default.asmx/GetCatalog",
            cache: false,
            contentType: "application/json; charset=utf-8",
            data: "{}",
            dataType: "json",
            success: handleHtml,
            error: ajaxFailed
        });
    });

    function handleHtml(data, status) {
        for (var count in data.d) {
            alert(data.d[count].Author);
            alert(data.d[count].BookName);
        }
    }

    function ajaxFailed(xmlRequest) {
        alert(xmlRequest.status + ' \n\r ' +
              xmlRequest.statusText + '\n\r' +
              xmlRequest.responseText);
    }
</script>

这篇关于从jQuery的调用ASMX的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 05:02