【问题标题】:Reading JSON values in servlet [duplicate]在 servlet 中读取 JSON 值 [重复]
【发布时间】:2013-07-22 02:09:08
【问题描述】:

我将 jQuery AJAX POST 发布到 servlet,并且数据采用 JSON 字符串的形式。 我不确定数据是否被发布。另外,我想通过从 json 对象中获取登录名和密码来验证它。

这里是sn-p的代码:

<script src="http://code.jquery.com/jquery-latest.min.js"></script>
    <script type="text/javascript" src="http://ajax.cdnjs.com/ajax/libs/json2/20110223/json2.js"></script>

    <script type="text/javascript">            
    function doajax(){

    var fullname=$("#fullname").val;
    var mobileno=$("#mobileno").val;


      $.ajax({
         url: 'dvsds',
         type: 'POST',
         dataType: 'json',
         data:{ "mobileno":mobileno, "fullname":fullname},
          error:function(){
              alert("error occured!!!");
          },
          success:function(data){
               alert(data.message);
         }
       });
      }
</script>

我的 servlet 端代码:

public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    response.setContentType("application/json");
    PrintWriter out = response.getWriter();
    try {
        String message=null;

        JSONObject jObj = new JSONObject(request.getParameter("jsondata"));
        Iterator<String> it = jObj.keys(); //gets all the keys

        String Name=null;
        String mobileno=null;

        while(it.hasNext())
        {
            String key = (String) it.next(); // get key

            if(key.equals("fullname"))
                 Name = (String)jObj.get(key);
            else if(key.equals("mobileno"))
                mobileno=(String)jObj.get(key);
        }


        if(Name=="abc" &&  mobileno=="123" )
            message="success";
        else
            message="fail";

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("message", message);
        out.println(jsonObject);

【问题讨论】:

    标签: ajax json servlets


    【解决方案1】:

    jQuery.Ajax 函数的datatype 属性声明如下:

    dataType(默认:Intelligent Guess(xml、json、script 或 html))

    类型:字符串

    您期望从服务器返回的数据类型。

    所以,您发送的不是 JSON 字符串。您发送的是普通的旧请求数据。

    您可以使用以下方法在您的 servlet 中获取此数据:

    String mobileno = request.getParameter("mobileno");
    

    【讨论】:

      【解决方案2】:

      您不需要对数据进行字符串化...只需将其作为普通的旧 javascript 对象发送...通过将数据类型指定为 json...jquery 将弄清楚如何在请求中打包数据

      无需在服务器端进行任何更改

      所以如果你的 AJAX 调用变成:

        $.ajax({
           url: 'dvsds',
           type: 'POST',
           dataType: 'json',
           data: jsondata,
            error:function(){
                alert("error occured!!!");
            },
            success:function(data){
                 alert(data.message);
           }
         });
      

      在 servlet 中检索它们

      String fname = request.getParameter("fullname");
      String mobileno = request.getParameter("mobileno");
      

      我认为您需要注意区分大小写

      已编辑

      我看到你能不能把你的脚本改成如下。

      <script type="text/javascript">            
      function doajax(){
      
      var fullname=$("#fullname").val;
      var mobileno=$("#mobileno").val;
      
      var postReqData = {}; // Create an empty object
      postReqData['mobileno'] = mobileno;
      postreqData['fullname'] = fullname;
      
      
        $.ajax({
           url: 'dvsds',
           type: 'POST',
           dataType: 'json',
           data: postReqData,
            error:function(){
                alert("error occured!!!");
            },
            success:function(data){
                 alert(data.message);
           }
         });
        }
      

      【讨论】:

      • 嗨,jsshah,我按照提到的那样做了,但仍然显示失败,因为从 servlet 响应返回的值。
      猜你喜欢
      • 2020-01-27
      • 2016-04-19
      • 1970-01-01
      • 2017-09-05
      • 2019-07-15
      • 1970-01-01
      • 2011-07-17
      • 2019-07-31
      • 2016-02-02
      相关资源
      最近更新 更多