这里有新鲜出炉的 Javascript 教程,程序狗速度看过来!
Javascript 是一种由 Netscape 的 LiveScript 发展而来的原型化继承的基于对象的动态类型的区分大小写的客户端脚本语言,主要目的是为了解决服务器端语言,比如 Perl,遗留的速度问题,为客户提供更流畅的浏览效果。
这篇文章主要介绍了 POST 一个 JSON 格式的数据给 Restful 服务实例详解的相关资料, 需要的朋友可以参考下
在 Android/Java 平台上实现 POST 一个 json 数据:
- JSONObject jsonObj = new JSONObject();
- jsonObj.put("username", username);
- jsonObj.put("apikey", apikey);
- // Create the POST object and add the parameters
- HttpPost httpPost = new HttpPost(url);
- StringEntity entity = new StringEntity(jsonObj.toString(), HTTP.UTF_8);
- entity.setContentType("application/json");
- httpPost.setEntity(entity);
- HttpClient client = new DefaultHttpClient();
- HttpResponse response = client.execute(httpPost);
用 curl 可执行如下命令:
- curl -l -H "Content-type: application/json" -X POST -d '{"phone":"13521389587","password":"test"}' http://domain/apis/users.json
用 jQuery:
- $.ajax({
- url:url,
- type:"POST",
- data:data,
- contentType:"application/json; charset=utf-8",
- dataType:"json",
- success: function(){
- ...
- }
- })
PHP 用 cUrl 实现:
- $data = array("name" => "Hagrid", "age" => "36");
- $data_string = json_encode($data);
- $ch = curl_init('http://api.local/rest/users');
- curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
- curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- curl_setopt($ch, CURLOPT_HTTPHEADER, array(
- 'Content-Type: application/json',
- 'Content-Length: ' . strlen($data_string))
- );
- $result = curl_exec($ch);
来源: http://www.phperz.com/article/17/0815/340231.html