1

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

$('.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);
    }

我已经调试过了,但它给了我错误 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

有人可以帮忙吗?

4

1 回答 1

0

嘿,我终于得到了答案

这是代码:

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);
}
于 2013-02-15T15:31:46.927 回答