Quantcast
Channel: ASP.NET AJAX + Ajax Control Toolkit (ACT)
Viewing all articles
Browse latest Browse all 5678

Call ASP.NET web service and return JSON using only html/javascript

$
0
0

I've implmented a pretty basic webservice as per below.  I did a quick test in ASP.NET AJAX using the ScriptManager and it returned JSON-formatted data perfectly.  I am now trying to do the same using just html/javascript with the XmlHttpRequest object.

Currently I've assigned the context-type to the XmlHttpRequest as is apparently required, and the webservice is "working", but only returning xml. [:O]

I'd really appreciate any help - or maybe even a better way to access the ASP.NET web service externally (i.e. outside of ASP.NET).  Thanks.

PART A: ASP.NET Webservice snippet

[WebService(Namespace = "http://microsoft.com/webservices/";
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ScriptService]
public class ScriptingService : OrYxBaseWebService {
    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public ScriptPersonRoleList GetActiveCategoryManagers() {
        OrYxAppStatus myStatus = BFManager.GetInstance().CreateStatus(GetDefaultRequest());
        return BFManager.ScriptingBF.GetActiveCategoryManagers(ref myStatus);
    }
}

 PART B: Index html

<html>
<head>
<title>Ajax Test Drive</title>
<script type="text/javascript" language="javascript">

   function makePOSTRequest(url, parameters) {
      http_request = false;
      if (window.XMLHttpRequest) { // Mozilla, Safari,...
         http_request = new XMLHttpRequest();
         if (http_request.overrideMimeType) {
            // set type accordingly to anticipated content type
            // http_request.overrideMimeType('text/xml');
            http_request.overrideMimeType('text/html');
         }
      } else if (window.ActiveXObject) { // IE
         try {
            http_request = new ActiveXObject("Msxml2.XMLHTTP");
         } catch (e) {
            try {
               http_request = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {
               alert("not good at all..");
            }
         }
      }
      if (!http_request) {
         alert('Cannot create XMLHTTP instance');
         return false;
      }
    
      http_request.onreadystatechange = confirmResponse;
      http_request.open('POST', url, true);
      http_request.setRequestHeader("Content-Type", "application/json");
     
      http_request.send(parameters);
   }
  
   function confirmResponse() {
      if (http_request.readyState == 4) {
         if (http_request.status == 200) {
            result = http_request.responseText;
            document.getElementById('databox').innerHTML = result;
            alert('Response:\n\n' + result);
         } else {
            alert('There was a problem with the request.');
         }
      }
   }

   function getData() {
      var url = 'http://localhost/TVScriptingWebService/TVScripting.asmx/GetActiveCategoryManagers';
      var request = makePOSTRequest(url, "");
   }
</script>

</head>
<body onload="getData()">
    <div style="width: 600px; padding: 10px; border: 1px solid;" align="center">
        Test page</div>
    <br>
    <div id="databox">
    </div>
    <br>
</body>
</html>


Viewing all articles
Browse latest Browse all 5678

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>