4

I am using apache tomcat as a web server. I have deployed webservices on tomcat. If i post request through jquery ajax from local file system to tomcat webservice in response i am getting 403 error.

If i run the same script from the same container i am getting valid response from the webservice.

I am using following code.

function callservice() 
    {
    jQuery.support.cors = true;
        var mobNo = document.getElementById('mobileNo').value;
        var acctNo = document.getElementById('accountNo').value;
        //var id = document.getElementById('id').value;
        var custNo = document.getElementById('customerId').value;

        //var mobNo = document.getElementById('txt_name').value;
        //alert("mobileNo" + mobNo+"accountNo" + acctNo+"customerId "+custNo);

        var url = "http://localhost/mobile-services/rest/user/";        
        var dataVal = {};
        dataVal["mobileNo"] = mobNo;
        dataVal["accountNo"] = acctNo;
        dataVal["customerId"] = custNo;

        var forminput = JSON.stringify(dataVal);

    $.ajax({
        url: url,
        type: "POST",
        data:forminput,
        processdata: true,
        contentType: "application/json;",
        beforeSend: function () { },
        headers : 
        {
            "Content-Type"  : "application/json",
            "Accept" : "application/json"               
        },
        success: function (data) 
        {          
            if (data.authenticated==true)
            {
                window.location.replace("http://localhost:8080/mobile-services/selected_services.jsp?userId="+data.id);
            }
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) 
        {
            try 
            {
                alert(JSON.stringify(XMLHttpRequest) + "\n" + textStatus + "\n" + errorThrown);
            }
            catch (ex) { alert("Exception occured.. "); }
            finally { }
        }
    });
}

Please suggest.

4

1 に答える 1

3

Web サーバーはクロスドメイン通信を想定しているため、403 を取得しているのはそのためです。

これにはJSONPを使用する必要があります

https://github.com/jaubourg/jquery-jsonp

基本的な使い方。

$.jsonp({
      var url  = "abc.do";
      success: function(jsonData, textStatus) {
          $.jsonp({
              url: url+"?callback=?",
              success: function(jsonData, textStatus) {

              },
              error: function(xOptions, textStatus){

              }
        });

      },
      error: function(xOptions, textStatus){

      }
});
于 2012-06-25T07:48:58.320 に答える