locked
Call external web service RRS feed

  • Question

  • User1850488 posted

    Hi,

    I need call external web service, always go in error.....

    my example:

    <script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.5.2.min.js"></script>
        <script type="text/javascript">
            function testJson() {
                $.ajax({
                    type: "POST",
                    url: "http://xxx.com/ws2.asmx/HelloWorld",
                    data: "{}",
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function (msg) {
                        alert(msg.d)
                    },
                    error: function (msg) {
                        alert("ERROR")
                    }
                });
            };
        </script>
       <input type="button" name="test" value="test" onclick="testJson();" />

    What is wrong in my source code?

    Monday, May 18, 2015 6:38 AM

Answers

All replies

  • User-484054684 posted

    You could have a glance at the error message to get the clues.

    To know what is the error, you can either enable developer tools or, check the msg variable which you've written in error: function(msg).

    To enable the developer tools, use shortcut key F12 for IE and Firefox.

    Also for firefox, if you are using Firebug extension, you are good to go.

    Monday, May 18, 2015 8:20 AM
  • User1850488 posted

    My service.asmx/HelloWorld  return

    <string xmlns="http://tempuri.org/">Hello World</string>

    When run from local this code:

    <script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.5.2.min.js"></script>
                <script type="text/javascript">
                    function testJson() {
    
                        $.ajax({
    
                            url: "http://ws.xxx.com/ws2.asmx/HelloWorld",
                            type: "POST",
                            datatype: "json",
                            contentType: "application/json; charset=utf-8",
                            success: function (msg) {
                                alert(msg['d']);
                            },
                            error: function(e) {
                                alert(JSON.stringify(e));
                            }
                        });
                    };
                </script>
                <input type="button" name="test" value="test" onclick="testJson()" />


    Monday, May 18, 2015 9:20 AM
  • User-484054684 posted

    Could it be due to cross-domain problem? Are you sure, your web application and webservice are under same tree in IIS?

    http://stackoverflow.com/questions/19395354/jquery-ajax-readystate-0-responsetext-status-0-statustext-error

    Or, a redirection code somewhere after ajax call? as in:

    http://stackoverflow.com/questions/25025465/tracking-ajax-error-on-readystate-0-status-0-and-statustext-error

    Monday, May 18, 2015 11:42 AM
  • User1850488 posted

    I have some error with free online webservice

    http://www.webservicex.net/BibleWebservice.asmx

    GetBookTitles

    <script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.5.2.min.js"></script>
            
           <script type="text/javascript">
               function testJson() {
     
     
                   $.ajax({
                       url: "http://www.webservicex.net/BibleWebservice.asmx/GetBookTitles?callback=?",          
                       type: "GET",
                       datatype: "jsonp",
                       
                       contentType: "application/json; charset=utf-8",
                       success: function (msg) {
                           alert(msg['d']);
                       },
                       error: function(e) {
                           alert(JSON.stringify(e));
                       }
                   });
               };
           </script>
           <input type="button" name="test" value="test" onclick="testJson()" />
    

    Tuesday, May 19, 2015 3:56 AM
  • User-484054684 posted

    Hi Joe,

    I saw that you are using JSONP, which is good. But, you would also need to make sure that the service (asmx) supports JsonP.

    Few articles on this topic that may help you understand:

    http://mac-blog.org.ua/asmx-jsonp/

    http://jquery-howto.blogspot.com/2013/09/jquery-cross-domain-ajax-request.html#jsonp (See Requirements & Notes section) which states that "In order to be able to use JSONP, the third party server must support it. In other words wrapping JSON data into a function call").

    Custom solutions: http://stackoverflow.com/questions/13884253/jsonp-asmx-web-service

    http://stackoverflow.com/questions/14221429/how-can-i-produce-jsonp-from-an-asp-net-web-service-for-cross-domain-calls

    You might find much on this, if you google with words like "Enable JSONP asmx"

    Regards,
    Siva

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, May 19, 2015 9:08 AM
  • User1534684113 posted

    Re: Call external web service

    05-19-2015 08:56 AM

    I have some error with free online webservice

    http://www.webservicex.net/BibleWebservice.asmx

    GetBookTitles

    Hi joe_t,

     

    Thank you for your post.

    According to your description,  I suspect it to be due to cross-domain problem.

     

    Console error messages in F12 developer tools, I find this error message "XMLHttpRequest for [URL] required CORS preflight. ".

    An XMLHttpRequest was made to a domain that was different than your page's domain.

    The specific operation of the request required both CORS support and preflighting on the server side, but preflighting failed.

     

    Note: This error code was removed in IE11 on Windows 10. It remains in IE11 for Windows 8.1 and Windows 7.

    The server or back-end script needs to support preflighting on CORS requests.

    Point the developer in charge of the server to CORS for XHR in IE10 for more info about CORS and preflighting in response headers.

     

    More details you can visit:

    https://msdn.microsoft.com/en-us/library/dn423949(v=vs.85).aspx

    Hoping my reply could be helpful to you.

    Friday, May 29, 2015 5:05 AM