2013-02-07 37 views
1

我試圖從jQuery函數調用ASMX服務在服務器類似如下:如何從jquery調用(asmx)http web服務?

$('.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內部服務器錯誤

和服務器描述像以下內容:

HTTP POST

以下是一個示例HTTP POST re追求和迴應。顯示的佔位符需要用實際值替換。

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 

任何人都可以幫忙嗎?

回答

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); 
}