【问题标题】:how to call (asmx) http web service from jquery?如何从 jquery 调用(asmx)http web 服务?
【发布时间】:2013-02-07 11:04:03
【问题描述】:

我正在尝试从 jQuery 函数调用 asmx 服务 On server,如下所示:

$('.myButton').click(function() {

                        $.ajax({
                                type: "POST",
                                url: "/http://myserver/service.asmx/GetProgramCategories",
                                cache: false,
                                contentType: "application/x-www-form-urlencoded",
                                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);
    }

我已经调试过了,但它给了我error 500 Internal Server Error

和服务器描述符如下:

HTTP POST

以下是一个示例 HTTP POST 请求和响应。显示的占位符需要替换为实际值。

POST /service.asmx/GetProgramCategories HTTP/1.1
Host: myhost
Content-Type: application/x-www-form-urlencoded
Content-Length: length

username=string&password=string

HTTP/1.1 200 OK

有人可以帮忙吗?

【问题讨论】:

    标签: jquery ajax webservice-client


    【解决方案1】:

    嘿嘿,终于找到答案了

    这是代码:

    function sendRequest(url,callback,postData) {
        var req = createXMLHTTPObject();
        if (!req) return;
        var method = (postData) ? "POST" : "GET";
        req.open(method,url,true);
        req.setRequestHeader('User-Agent','XMLHTTP/1.0');
        if (postData)
            req.setRequestHeader('Content-type','application/x-www-form-urlencoded');
        req.onreadystatechange = function () {
            if (req.readyState != 4) return;
            if (req.status != 200 && req.status != 304) {
    //          alert('HTTP error ' + req.status);
                return;
            }
            callback(req);
        }
        if (req.readyState == 4) return;
        req.send(postData);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-03-31
      • 2013-03-27
      • 1970-01-01
      • 1970-01-01
      • 2014-02-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多