如何调用Restfull Web服务并传递JSON数据并以字符串形式获取响应

时间:2018-09-18 12:00:09

标签: javascript jquery html

错误

  

jquery-1.7.js:8157选项http://localhost:8080/DigitalWallet/subscriber/add/ 403   发送@ jquery-1.7.js:8157
  ajax @ jquery-1.7.js:7635
  (匿名)@ CallTest.js:9
  派遣@ jquery-1.7.js:3319
  eventHandle @ jquery-1.7.js:2925
  index.html:1无法加载http://localhost:8080/DigitalWallet/subscriber/add/:飞行前响应没有HTTP正常状态。

我的客户代码是

$(document).ready(function() {

  $('#butCallAjax').click(function() {
    jQuery.support.cors = true;
    alert("awa");
    var data0 = {
      number: '0719402280',
      agent_number: '0719402243',
      agent_pin: '1234'
    };

    var json = JSON.stringify(data0);
    $.ajax({
      type: "POST",
      dataType: "text",
      data: json,
      contentType: "application/json; charset=utf-8",
      url: "http://localhost:8080/DigitalWallet/subscriber/add/",
      success: function(data) {
        alert(data);
      },
      error: function(msg, url, line) {
        alert('error trapped in error: function(msg, url, line)');
        alert('msg = ' + msg + ', url = ' + url + ', line = ' + line);

      }

    });
  });
});

这是我的用户HTML部分

<html>

<head>
  <title>TODO supply a title</title>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <script src="http://code.jquery.com/jquery-1.7.js" type="text/javascript"></script>
  <script src="js/custom/CallTest.js" type="text/javascript"></script>
</head>
<body>
  <button id="butCallAjax">Get data</button>
  <select id="cboFastBikes" name="cboFastBikes"></select>
</body>
</html>

这是我的服务

@RequestMapping(value = "/agent/add/", method = RequestMethod.POST)
public String addAgent(@RequestBody Agent agent) throws Exception {
    Log4JUtil.logger.info("Agent,agent_save_request,Request=" + agent.toString()+"|"+agent.getName()+"|"+agent.getNumber());
    String Text = "";
    try {
        agentValidation.fieldValidation(agent);
        String number = agent.getNumber();
        try {
            Agent dbagent = agentService.findByNumber(number);
            if (dbagent != null) {
                throw new AgentAlreadyExistsException();
            } else {
                agentService.saveOrUpdate(agent);
                agentService.createWallet(number);
                Text = "0|Created|Active|Agnt";
            }
        } catch (Exception exp) {
            throw exp;
        }
    } catch (Exception ex) {
        Log4JUtil.logger.info("Agent,agent_save_request,Request=" + agent.toString() + "," + ex.toString());
        throw  ex;
    }
    return Text;
}

此服务运行正常。我用邮递员检查了服务。请帮助我找出这个问题

0 个答案:

没有答案