简体   繁体   中英

Access denied when calling web service from javascript

My goal is to call a Web Service using Jquery. i found this example from this site: How to use jQuery to call an ASP.NET web service?

i modified it to my use:

function InfoByDate(aUrl){
    var divToBeWorkedOn = '#AjaxPlaceHolder';
    var webMethod = 'http://MyWebService/Web.asmx/GetInfoByDates'
    var parameters = "{'sUrl':'" + aUrl + "'}"

    $.ajax({
        type: "POST",
        url: webMethod,
        data: parameters,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(msg) {    
            $(divToBeWorkedOn).html(msg.d);
        },
        error: function(e){
            $(divToBeWorkedOn).html("Unavailable");              
        }
    });
}

Now i get back a js error access denied. my web service is located in the same domain so cross domain is not relevant? Can someone help me with that issue? Cheers NirNiroN

If you're using JavaScript and your not calling the web-service from your own domain, then it wont be allowed. You can use JSONP for cross domain calls however, so try to use that.

You can read up on it here http://en.wikipedia.org/wiki/JSONP

Where are you calling this from? If you're not on the same domain (ie "mywebservice"), it won't work as you can't do cross-domain ajax calls like that.

If this is the case, I suggest looking into the various options available to you (CORS on the server, using a reverse proxy on the 'client' machine, or JSONP).

Is the webservice available on the same server? Cross server requests are not allowed with Ajax and your webservice must be in the same domain as your JavaScript code to allow it to work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM