【问题标题】:Ajax jquery call getting NetworkError: 403 Forbidden error in responseAjax jquery 调用获取 NetworkError: 403 Forbidden error in response
【发布时间】:2012-06-25 07:43:28
【问题描述】:

我正在使用 apache tomcat 作为 Web 服务器。 我已经在 tomcat 上部署了 web 服务。 如果我通过 jquery ajax 从本地文件系统向 tomcat webservice 发布请求作为响应,我会收到 403 错误。

如果我从同一个容器运行相同的脚本,我会从 web 服务获得有效的响应。

我正在使用以下代码。

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

请提出建议。

【问题讨论】:

  • 您是否在 apache 上部署了 tomcat 或者它是独立的?我假设您通过 apache 将您的请求路由到 tomcat,因为您已将您的 url 指定为 localhost
  • 您可以尝试在 $.ajax 请求中添加 dataType: "jsonp", jsonp: "callbackname" 吗?
  • 它是一个独立的安装。我尝试使用数据类型为 jsonp。我得到方法不允许的异常。

标签: jquery ajax web-services tomcat


【解决方案1】:

因为网络服务器假设它是跨域通信,这就是你得到 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){

      }
});

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-26
  • 2017-12-30
  • 1970-01-01
  • 1970-01-01
  • 2021-02-11
  • 1970-01-01
相关资源
最近更新 更多