zoukankan      html  css  js  c++  java
  • ajax请求返回json字符串/json对象 处理

    1. 返回json字符串如何处理

    $.ajax({

    url:xxx,

    success:function(date){

    },

    error:function(){

    }

    });

    通过最原始的返回:

    PrintWriter out = httpServletResponse.getWriter();

    ObjectMapper mapper = new ObjectMapper();

    Map map = new HashMap();

    map.put("name","zz");

    out.write(mapper.writeValueAsString(map));

    如果通过此方法返回,ajax中date 是个json格式的字符串,不是json对象。 在处理数据时需要(使用jquery对json字符串的解析方法):

    success:function(date){

    var jsonObj = $.parseJSON(date);

    console.log(date.name);

    jQuery.parseJSON('{"name":"John"}');

    }

    2. 如何返回json对象,而非json字符串

    在ajax中增加如下:

    dataType: "json"   ,//请求页面返回的数据类型 

  • 相关阅读:
    Linux如何自动获取IP地址
    jq操作select集合
    UDP and TCP
    IPv6
    DHCP, NAT
    队列之顺序存储实现
    ARP, Fragmentation and Reassembly
    Classless Interdomain Routing (CIDR)
    Subnet Routing Examples
    Subnetting
  • 原文地址:https://www.cnblogs.com/huangjinyong/p/9208094.html
Copyright © 2011-2022 走看看