开发的时候,会经常使用一些假数据,这个时候我们就会在本地进行ajax请求(开发工具使用vscode,如果是webstrom则不会有这个问题),来获取数据,例如ajax本地请求json文件,但是在请求的时候会遇到跨域问题:
建议使用火狐浏览器,因为谷歌浏览器是不允许跨域请求的,因为不安全,但是我们现实中,很多地方是必须要用到跨域请求,谷歌浏览器严格遵循,但是火狐浏览器考虑到现实因素,就可以使用跨域,但是需要进行一些设置.
1、在Firefox的地址栏输入“about:config”,回车
2、在过滤器(filter)中搜索“security.fileuri.strict_origin_policy”(第一次会出现安全协议,点击确认即可)
3、双击security.fileuri.strict_origin_policy设置为false
4、关闭目前开启的所有Firefox窗口,然后重新启动Firefox,这个时候就可以了.
虽然会报格式不佳的错误,但是数据还是请求出来滴!
❷ JSON请求范例
目录主题帖传送门: Spring SpringMVC MyBatis 整合-重复的轮子造的不亦乐乎 -
上一篇: MVC Post/Get 页面请求范例 -
范例基于SSM框架,如何搭建移步: 拉Jar包方式SSM框架搭建 -
jar包是jackson,我们搭建SSM框架包含了磨盯,那么JSON请求这就简单了,我们以列出用户列表为例,在controller里面添加一个方法迟蔽用于返回JSON类型的数据。
很眼熟,内容基本和 MyBatis注解开发基本范例 - 里的listAllUsers方法一样不同的是@ResponseBody告诉码游州Spring直接返回数据,返回类型Object是Map会被解释为键值对。
实际效果:返回JSON,Response-Head中Content-Type类型是application/json
这里有个奇葩问题,就是Jackson返回XML并非JSON,为什么呢?因为多加了一个包,Jackson目录下还有个format-xml的包,如果添加了这个jar,那返回的就是XML
下一篇: 单文件/多文件上传 请求范例 -
❸ 如何从post json数据到网站
1. JSON的数据格式
a) 按照最简单的形式,可以用下面这样的 JSON 表示名称/值对:
{ "firstName": "Brett" }
b) 可以创建包含多个名称/值对的记录,比如:
{ "firstName": "Brett", "lastName":"McLaughlin", "email": "[email protected]" }
c) 可以创建值的数组
{ "people": [
{ "firstName": "Brett", "lastName":"McLaughlin", "email": "谈脊衡[email protected]" },
{ "firstName": "Jason", "lastName":"Hunter", "email": "[email protected]" }
]}
d) 当然,可以使用相同的语法表示多个值(每个值包含多个记录):
{ "programmers": [
{ "firstName": "Brett", "lastName":"McLaughlin", "email": "[email protected]" },
{ "firstName": "Jason", "lastName":"Hunter", "email": "[email protected]" }
],
"authors": [
{ "firstName": "Isaac", "lastName": "Asimov", "genre": "science fiction" },
{ "firstName": "Tad", "lastName": "Williams", "genre": "fantasy" }
],
"musicians": [
{ "firstName": "Eric", "lastName": "Clapton", "instrument": "guitar" }
]
}
注意,在不同的主条目(programmers、authors 和 musicians)之间野誉,记录中实际的名称/值对可以不一样。JSON 是完全动态的,允许含做在 JSON 结构的中间改变表示数据的方式。
2. 在 javaScript 中使用 JSON
JSON 是 JavaScript 原生格式,这意味着在 JavaScript 中处理 JSON 数据不需要任何特殊的 API 或工具包。
2.1 将 JSON 数据赋值给变量
例如,可以创建一个新的 JavaScript 变量,然后将 JSON 格式的数据字符串直接赋值给它:
var people =
{ "programmers": [
{ "firstName": "Brett", "lastName":"McLaughlin", "email": "[email protected]" },
{ "firstName": "Jason", "lastName":"Hunter", "email": "[email protected]" }
],
"authors": [
{ "firstName": "Isaac", "lastName": "Asimov", "genre": "science fiction" },
{ "firstName": "Tad", "lastName": "Williams", "genre": "fantasy" }
],
"musicians": [
{ "firstName": "Eric", "lastName": "Clapton", "instrument": "guitar" }
]
}
2.2 访问数据
将这个数组放进 JavaScript 变量之后,就可以很轻松地访问它。实际上,只需用点号表示法来表示数组元素。所以,要想访问 programmers 列表的第一个条目的姓氏,只需在JavaScript 中使用下面这样的代码:
people.programmers[0].lastName;
注意,数组索引是从零开始的。
2.3 修改 JSON 数据
正如访问数据,可以按照同样的方式修改数据:
people.musicians[1].lastName = "Rachmaninov";
2.4 转换回字符串
a) 在 JavaScript 中这种转换也很简单:
String newJSONtext = people.toJSONString();
b) 可以将任何 JavaScript 对象转换为 JSON 文本。并非只能处理原来用 JSON 字符串赋值的变量。为了对名为 myObject 的对象进行转换,只需执行相同形式的命令:
String myObjectInJSON = myObject.toJSONString();
说明:将转换回的字符串作为Ajax调用的字符串,完成异步传输。
小结:如果要处理大量 JavaScript 对象,那么 JSON 几乎肯定是一个好选择,这样就可以轻松地将数据转换为可以在请求中发送给服务器端程序的格式。
3. 服务器端的 JSON
3.1 将 JSON 发给服务器
a) 通过 GET 以名称/值对发送 JSON
在 JSON 数据中会有空格和各种字符,Web 浏览器往往要尝试对其继续编译。要确保这些字符不会在服务器上(或者在将数据发送给服务器的过程中)引起混乱,需要在JavaScript的escape()函数中做如下添加:
var url = "organizePeople.php?people=" + escape(people.toJSONString());
request.open("GET", url, true);
request.onreadystatechange = updatePage;
request.send(null);
b) 利用 POST 请求发送 JSON 数据
当决定使用 POST 请求将 JSON 数据发送给服务器时,并不需要对代码进行大量更改,如下所示:
var url = "organizePeople.php?timeStamp=" + new Date().getTime();
request.open("POST", url, true);
request.onreadystatechange = updatePage;
request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
request.send(people.toJSONString());
注意:赋值时格式必须是var msg=eval('(' + req.responseText + ')');
3.2 在服务器上解释 JSON
a) 处理 JSON 的两步骤。
针对编写服务器端程序所用的语言,找到相应的 JSON 解析器/工具箱/帮助器 API。
使用 JSON 解析器/工具箱/帮助器 API 取得来自客户机的请求数据并将数据转变成脚本能理解的东西。
b) 寻找 JSON 解析器
寻找 JSON 解析器或工具箱最好的资源是 JSON 站点。如果使用的是 Java servlet,json.org 上的 org.json 包就是个不错的选择。在这种情况下,可以从 JSON Web 站点下载 json.zip 并将其中包含的源文件添加到项目构建目录。编译完这些文件后,一切就就绪了。对于所支持的其他语言,同样可以使用相同的步骤;使用何种语言取决于您对该语言的精通程度,最好使用您所熟悉的语言。
c) 使用 JSON 解析器
一旦获得了程序可用的资源,剩下的事就是找到合适的方法进行调用。如果在 servlet 中使用的是 org.json 包,则会使用如下代码:
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
StringBuffer jb = new StringBuffer();
String line = null;
try {
BufferedReader reader = request.getReader();
while ((line = reader.readLine()) != null)
jb.append(line);
} catch (Exception e) { //report an error }
try {
JSONObject jsonObject = new JSONObject(jb.toString());
} catch (ParseException e) {
// crash and burn
throw new IOException("Error parsing JSON request string");
}
// Work with the data using methods like...
// int someInt = jsonObject.getInt("intParamName");
// String someString = jsonObject.getString("stringParamName");
// JSONObject nestedObj = jsonObject.getJSONObject("nestedObjName");
// JSONArray arr = jsonObject.getJSONArray("arrayParamName");
// etc...
}
❹ fetch请求json数据的基本步骤
fetch请求json数据的基本步骤如下
1.以简单的方式获取JSONP
2.设置JSONP回调参数名称,默认为’回调’
3.设置JSONP回调函数名称,默认为带json_前缀的随机数
4.设置JSONP请求超时,默认为5000ms
jsonpCallback和之间的区别jsonpCallbackFunction:
jsonpCallback,默认值是callback;它是回调参数的名称
jsonCallbackFunction,默认值是null;它是回调函数的名称。为了使它与众不同,它是一个带有jsonp_前缀的随机字符串jsonp_1497658186785_39551。如果由服务器设置,则将其留空,如果回调函数名称是固定的,则将其明确设置。
❺ 为什么ajax请求json数据,在IE浏览器里面不能正常显示
因为IE把application/json当作是文件,
可以通过设置ContentType为text/html来解决IE请求JSON出现的下载问题
❻ 请求参数是json springmvc 怎么注解
解决方案:
类似于之前写的《扩展SpringMVC以支持更精准的数据绑定》,扩展spring的HandlerMethodArgumentResolver以支持自定义的数据绑定方式。
1、请下载附件的代码,放到工程中;
2、在RequestMappingHandlerAdapter添加自定义HandlerMethodArgumentResolver Bean;
Java代码
<span style="font-size: x-small;"> <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<!--线程安全的访问session-->
<property name="synchronizeOnSession" value="true"/>
<property name="customArgumentResolvers">
<list>
<bean class="cn.javass.spring.mvc.method.annotation."/>
<bean class="cn.javass.spring.mvc.method.annotation."/>
</list>
</property>
</bean> </span>
//customArgumentResolvers用于注入自定义的参数解析器,此处我们注入了。
3、使用方式
Java代码
<span style="font-size: x-small;">@RequestMapping("/list")
public String list(@RequestJsonParam("list") List<Integer> list) </span>
4、测试控制器
Java代码
<span style="font-size: x-small;">package cn.javass.chapter6.web.controller.jsonparam;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import cn.javass.chapter6.model.DataBinderTestModel;
import cn.javass.chapter6.model.UserModel;
import cn.javass.spring.mvc.bind.annotation.RequestJsonParam;
import cn.javass.spring.mvc.util.MapWapper;
@Controller
@RequestMapping("/jsonparam")
public class JsonParamController {
//ok http://localhost:9080/springmvc-chapter6/jsonparam/list?list=[1,2,34]
//fail http://localhost:9080/springmvc-chapter6/jsonparam/list?list=[1,2,a]
@RequestMapping("/list")
public String list(@RequestJsonParam("list") List<Integer> list) {
System.out.println(list);
return "redirect:/success";
}
//ok http://localhost:9080/springmvc-chapter6/jsonparam/set?set=[1,2,34]
//fail http://localhost:9080/springmvc-chapter6/jsonparam/set?set=[1,2,a]
@RequestMapping("/set")
public String set(@RequestJsonParam("set") Set<Integer> set) {
System.out.println(set);
return "redirect:/success";
}
//ok http://localhost:9080/springmvc-chapter6/jsonparam/array?array=[1,2,3]
//fail http://localhost:9080/springmvc-chapter6/jsonparam/array?array=[1,2,a]
@RequestMapping("/array")
public String list(@RequestJsonParam("array") int[] array) {
System.out.println(Arrays.toString(array));
return "redirect:/success";
}
//ok http://localhost:9080/springmvc-chapter6/jsonparam/map?map={"a":1, "b":2}
//fail http://localhost:9080/springmvc-chapter6/jsonparam/map?map={"a":1, "b":a}
@RequestMapping("/map")
public String map(@RequestJsonParam(value = "map", required=false) MapWapper<String, Integer> map) {
System.out.println(map);
return "redirect:/success";
}
//UserModel[]
//ok http://localhost:9080/springmvc-chapter6/jsonparam/array2?array=[{"username":"123"},{"username":"234"}]
@RequestMapping("/array2")
public String array2(@RequestJsonParam(value = "array") UserModel[] array) {
System.out.println(Arrays.toString(array));
return "redirect:/success";
}
//List<UserModel>
//ok http://localhost:9080/springmvc-chapter6/jsonparam/list2?list=[{"username":"123"},{"username":"234"}]
@RequestMapping("/list2")
public String list2(@RequestJsonParam(value = "list") List<UserModel> list) {
System.out.println(list);
return "redirect:/success";
}
//Set<UserModel>
//ok http://localhost:9080/springmvc-chapter6/jsonparam/set2?set=[{"username":"123"},{"username":"234"}]
@RequestMapping("/set2")
public String set2(@RequestJsonParam(value = "set") Set<UserModel> set) {
System.out.println(set);
return "redirect:/success";
}
//Map<String, UserModel>
//ok http://localhost:9080/springmvc-chapter6/jsonparam/map2?map={"a":{"username":"123"},"b":{"username":"234"}}
//暂不支持 Map<UserModel, UserModel>
@RequestMapping("/map2")
public String map2(@RequestJsonParam(value = "map") MapWapper<String, UserModel> map) {
System.out.println(map);
return "redirect:/success";
}
//ok http://localhost:9080/springmvc-chapter6/jsonparam/model1?model={"username":123,"password":234,"realname":"zhang","workInfo":{"city":"abc","job":"abc","year":"abc"}, "schoolInfo":{"schoolType":"1","schoolName":"1","specialty":"1"}}
//没有realname1
//fail http://localhost:9080/springmvc-chapter6/jsonparam/model1?model={"username":123,"password":234,"realname1":123}
@RequestMapping("/model1")
public String model1(@RequestJsonParam(value = "model", required=true) UserModel user) {
System.out.println(user);
return "redirect:/success";
}
//ENUM
//ok http://localhost:9080/springmvc-chapter6/jsonparam/model2?model={"state":"normal"}
//List<基本类型>
//ok http://localhost:9080/springmvc-chapter6/jsonparam/model2?model={"hobbyList":["film", "music"]}
//Map<基本类型,基本类型>
//ok http://localhost:9080/springmvc-chapter6/jsonparam/model2?model={"map":{"key":"value", "a":"b"}}
@RequestMapping("/model2")
public String model2(@RequestJsonParam(value = "model", required=true) DataBinderTestModel model) {
System.out.println(model);
return "redirect:/success";
}
//List<UserModel>
//ok http://localhost:9080/springmvc-chapter6/jsonparam/model3?model={"userList":[{"username":"1"},{"username":"2"}]}
//Map<String,UserModel>
//ok http://localhost:9080/springmvc-chapter6/jsonparam/model3?model={"userMap":{"1":{"username":"1"},"2":{"username":"2"}}}
//暂不支持 类似于 Map<UserModel, UserModel> 形式
@RequestMapping("/model3")
public String model3(@RequestJsonParam(value = "model") DataBinderTestModel model) {
System.out.println(model);
return "redirect:/success";
}
}
</span>
支持的spring版本:
springmvc 3.0 和 3.1.x。
支持绑定的数据:
模型、集合、数组、MapWapper(Map的一个包装器,通过getInnerMap获取真实Map)
❼ iPad下载json文件直接打开了
正确打开如下。
使用TouchJSon解析方法,将解析得到的内容存放字典中,编码格式为UTF。(2)使用SBJson解析方法,(3)使用IOS5自带解析类NSJSONSerialization方法解析。
json是一个HTTP代理/HTTP监视器/反向代理,使开发人员能够查看其机器和Internet之间的所有HTTP和SSL/HTTPS流量。这包括请求,响应和HTTP标头(包含cookie和缓存信息)Charles是在Mac下常用的网络封包截取工具。
在做移动开发时,我们为了调试与服务器端的网络通讯协议,常常需要截取网络封包来分析。通过将自己设置成系统的网络访问代理服务器,使得所有的网络访问请求都通过它来完成,从而实现了网络封包的截取和分析。除了在做移动开发中调试端口外。
Charles也可以用于分析第三方应用的通讯协议。配合Charles的SSL功能,Charles还可以分析Https协议下载Charles并不是一款免费产品,你需要破解才能使用,建议购买正版软件。这里使用的是文件覆盖的方法。
即:下载新的json文件,并在Charles的安装目录下替换掉它,Windows下替换目录在Charleslib破解的json.jar文件可以在网上搜索下载。
❽ asp 如何请求 json
传统的ASP与ASP之间post提交json可以用:
json=cstr(request.form)
来获取得到的json代码
2
实际上,如果是java或php提交过来的话,用request.form可能得到的就是空值,最稳妥的办法是根据二进制流得到数据,具体操作如下:
3
2个页面,第一个页面假设为:funtion.asp
代码如下:
<%
functionbytes2bstr(vin)
dimbytesstream,stringreturn
setbytesstream=server.CreateObject("adodb.stream")
bytesstream.type=2
bytesstream.open
bytesstream.writeTextvin
bytesstream.position=0
bytesstream.charset="utf-8"'或者gb2312
bytesstream.position=2
stringreturn=bytesstream.readtext
bytesstream.close
setbytesstream=nothing
bytes2bstr=stringreturn
endfunction
%>
4
第二个页面,假设为demo.asp,代码如下:
<!--#includefile="funtion.asp"-->
<%
getpostjson=Request.TotalBytes'得到字节数
ifgetpostjson=0then
response.Write("jsonnull")
response.End()
endif
readjson=Request.BinaryRead(getpostjson)'二进制方式来读取客户端使用POST传送方法所传递的数据
json=bytes2bstr(readjson)'二进制转化
response.write(json)
%>
5
字符串解析:
Set jsonobj=getJSONObject(json)