网络请求Request

appcan.ajax(options)

发起一个ajax请求,并获取相应的内容

  1. options:发起ajax的请求的参数,这个必须是一个对象
  2. options.type:请求的类型,包括GETPOST
  3. options.appVerify:是否在请求头中加入appVerify字段 truefalse
  4. options.certificate({password:'',path:'default'})添加证书信息
  5.   password:数字证书密码。当使用appcan默认证书时,此参数为空(或””)
  6.   path:路径,支持 file://,res://,wgt://等协议路径,详见CONSTANT中PathTypes。 当传入‘default’时,本次请求将取appcan默认数字证书。
  7. options.url:要请求的地址 注:get方式请求中携带中文参数,需要对参数进行encode编码,具体函数:encodeURIComponent
  8. options.data:要请求的URL的参数,如果要上传文件则data数据中必须传一个对象包含一个pathkey 例如:data:{file:{path:'a.jpeg'},file2:{path:'b.jpeg'}}上传a.jpeg,b.jpeg图片
  9. options.contentType:默认: false 要传给服务端的数据内容的'content-Type'通过header,如果设置其他content将会直接把data发送出去
  10. options.dataType:服务端的响应类型,包括json, jsonp, script, xml, html, text中的一种
  11. options.timeout:请求的超时时间
  12. options.headers:要设置的请求头
  13. options.xhrFields:要重载的请求对象的字段
  14. options.beforeSend(xhr, settings):请求发送之前的回调,返回false取消请求发送
  15. options.success(data, status,,requestCode,response, xhr):请求发送成功后的回调
  16.   requestCode: 服务器响应状态码,成功为200,失败为404,-1等;
  17.  reponse: JSON格式字符串:
  18. {
  19. "responseHeaders": “”,//请求头
  20. "responseStatusCode": “”,//状态码
  21. "responseStatusMessage": “”,//状态码的信息
  22. "responseError": “”//错误信息
  23. }
  24. options.error(xhr, errorType, error,msg):请求如果出现错误后的回调;msg: 错误详细信息,服务器返回的result信息
  25. options.complete(xhr, status):请求完成后的回调,不管是否出错
  26. options.progress(progress, xhr):上传的进度,只有包含上传文件的时候才会执行该回调
  27. options.cache:是否缓存请求
  28. options.offline:是否直接调用离线数据,包括true,false,undefined
  29.    **offline参数说明:**
  30.      true: 直接调用缓存数据,如果缓存数据不存在,执行ajax请求并离线缓存返回数据;
  31.      false: 直接请求ajax数据,并把请求到的数据离线缓存;
  32.     undefined: 直接请求ajax数据,不缓存请求到的数据;
  33. options.offlineDataPath:自定义离线数据文件存储目录
  34. options.expires:离线缓存过期时间,支持类型:
  35.  1.number类型(单位:毫秒),缓存过期时长,
  36.  2.W3c IOS 8601 DateTime格式,详见http://www.w3.org/TR/NOTE-datetime ,缓存过期时间点
  37. options.crypto:true/false 离线缓存时是否加密
  38. options.password: "string" 离线缓存加密密码

例如:

  1. appcan.ajax({
  2. url : 'http://weixin.appcan.cn:8086/test/get',
  3. type : 'GET',
  4. data : {
  5. a : 'hello word',
  6. b : 'page'
  7. },
  8. offline : true,
  9. offlineDataPath : 'wgt://aaa/',
  10. success : function(data) {
  11. alert(data);
  12. },
  13. error : function(e) {
  14. alert(e);
  15. }
  16. });

例如:

  1. A.number格式
  2. appcan.ajax({
  3. url : 'http://weixin.appcan.cn:8086/test/get',
  4. type : 'GET',
  5. data : {
  6. a : 'hello word',
  7. b : 'page'
  8. },
  9. offline : true,
  10. expires:3000,
  11. success : function(data) {
  12. alert(data);
  13. }, error : function(e) {
  14. alert(e);
  15. }
  16. });
  17. B.ISO 8601格式
  18. appcan.ajax({
  19. url : 'http://weixin.appcan.cn:8086/test/get',
  20. type : 'GET',
  21. data : {
  22. a : 'hello word',
  23. b : 'page'
  24. },
  25. offline : true,
  26. expires : '2015-05-16 ',
  27. success : function(data) {
  28. alert(data);
  29. },
  30. error : function(e) {
  31. alert(e);
  32. }
  33. });

例如:

  1. appcan.ajax({
  2. url:http://115.29.138.150:8086/test/get,
  3. type:"GET",
  4. data:{}, dataType:"json",
  5. timeout:30000,
  6. success:function(data, status, requestCode, response, xhr) {
  7. alert("status:" + status);
  8. alert("result:" + data);
  9. alert("requestCode:" + requestCode);
  10. alert("response:" + JSON.stringify(response));
  11. alert("xhr:" + JSON.stringify(xhr));
  12. }, error:function(xhr,erroType,error,msg) {
  13. alert("erroType:" + erroType);
  14. alert("error:" + error);
  15. alert("msg:" + msg);
  16. alert("xhr:" + JSON.stringify(xhr));
  17. }
  18. });
  19. //获取appcan.cn页面
  20. appcan.ajax({
  21. type : 'GET',
  22. url : 'http://appcan.cn',
  23. //添加参数
  24. data : {
  25. name : 'appcan'
  26. },
  27. //期望的返回类型
  28. dataType : 'html',
  29. timeout : 300, //超时时间
  30. success : function(data) {
  31. //获取内容
  32. alert('data');
  33. },
  34. error : function(xhr, type) {
  35. alert('Ajax error!')
  36. },
  37. offline : true
  38. })

离线缓存加解密例子:

  1. appcan.ajax({
  2. url : "http://weixin.appcan.cn:8086/test/get",
  3. type : "GET",
  4. data : {
  5. a : 'hello word',
  6. b : 'page'
  7. },
  8. dataType : "json",
  9. timeout : 30000,
  10. offline : true,
  11. crypto : true,
  12. password : "pwd",
  13. success : function(data, status, requestCode, response, xhr) {
  14. alert("status:" + status);
  15. alert("result:" + data);
  16. alert("requestCode:" + requestCode);
  17. alert("response:" + JSON.stringify(response));
  18. alert("xhr:" + JSON.stringify(xhr));
  19. },
  20. error : function(xhr, erroType, error, msg) {
  21. alert("erroType:" + erroType);
  22. alert("error:" + error);
  23. alert("msg:" + msg);
  24. alert("xhr:" + JSON.stringify(xhr));
  25. }
  26. });

appcan.request.ajax(options)

请参考 :appcan.ajax(options)发起一个ajax请求,并获取相应的内容

  1. options:发起ajax的请求的参数,这个必须是一个对象
  2. options.type:请求的类型,包括GETPOST
  3. options.appVerify:是否在请求头中加入appVerify字段 truefalse
  4. options.url:要请求的地址 注:get方式请求中携带中文参数,需要对参数进行encode编码,具体函数:encodeURIComponent
  5. options.data:要请求的URL的参数,如果要上传文件则data数据中必须传一个对象包含一个pathkey 例如:data:{file:{path:'a.jpeg'}}上传a.jpeg图片
  6. options.contentType:默认: false 要传给服务端的数据内容的'content-Type'通过header,如果设置其他content将会直接把data发送出去
  7. options.dataType:服务端的响应类型,包括json, jsonp, script, xml, html, text中的一种
  8. options.timeout:请求的超时时间
  9. options.headers:要设置的请求头
  10. options.xhrFields:要重载的请求对象的字段
  11. options.beforeSend(xhr, settings):请求发送之前的回调,返回false取消请求发送
  12. options.success(data, status,requestCode,response, xhr):请求发送成功后的回调 
  13.   **requestCodereponse说明:** 
  14.     requestCode: 服务器响应状态码,成功为200,失败为404,-1等;
  15.     reponse: JSON格式字符串:
  16.    {
  17.     "responseHeaders": “”,//请求头
  18.     "responseStatusCode": “”,//状态码
  19.     "responseStatusMessage": “”,//状态码的信息
  20.     "responseError": “”//错误信息
  21.    }
  22. options.error(xhr, errorType, error,msg):请求如果出现错误后的回调;msg: 错误详细信息,服务器返回的result信息
  23. options.complete(xhr, status):请求完成后的回调,不管是否出错
  24. options.progress(progress, xhr):上传的进度,只有包含上传文件的时候才会执行该回调
  25. options.certificate:添加证书信息 {password:'',path:''}其中password是证书的密码,path是证书的地址
  26. options.cache:是否缓存请求
  27. options.offline:是否直接调用离线数据,包括true,false,undefined
  28.   **offline参数说明:**
  29.    true: 直接调用缓存数据,如果缓存数据不存在,执行ajax请求并离线缓存返回数据;
  30.   false: 直接请求ajax数据,并把请求到的数据离线缓存;
  31.   undefined: 直接请求ajax数据,不缓存请求到的数据;
  32. options.offlineDataPath:自定义离线数据文件存储目录
  33. options.expires:离线缓存过期时间,支持类型:
  34.  1.number类型(单位:毫秒),缓存过期时长,
  35.  2.W3c IOS 8601 DateTime格式,详见http://www.w3.org/TR/NOTE-datetime ,缓存过期时间点
  36. options.crypto:true/false 离线缓存时是否加密
  37. options.password: "string" 离线缓存加密密码

例如:

  1. appcan.ajax({
  2. url : 'http://weixin.appcan.cn:8086/test/get',
  3. type : 'GET',
  4. data : {
  5. a : 'hello word',
  6. b : 'page'
  7. },
  8. offline : true,
  9. offlineDataPath : 'wgt://aaa/',
  10. success : function(data) {
  11. alert(data);
  12. },
  13. error : function(e) {
  14. alert(e);
  15. }
  16. });
  17. 例如:
  18. A.number格式
  19. appcan.ajax({
  20. url : 'http://weixin.appcan.cn:8086/test/get',
  21. type : 'GET',
  22. data : {
  23. a : 'hello word',
  24. b : 'page'
  25. },
  26. offline : true,
  27. expires:3000,
  28. success : function(data) {
  29. alert(data);
  30. }, error : function(e) {
  31. alert(e);
  32. }
  33. });
  34. B.ISO 8601格式
  35. appcan.request.ajax({
  36. url : 'http://weixin.appcan.cn:8086/test/get',
  37. type : 'GET',
  38. data : {
  39. a : 'hello word',
  40. b : 'page'
  41. },
  42. offline : true,
  43. expires : '2015-05-16 ',
  44. success : function(data) {
  45. alert(data);
  46. },
  47. error : function(e) {
  48. alert(e);
  49. }
  50. });

例如:

  1. appcan.request.ajax({
  2. url:http://115.29.138.150:8086/test/get,
  3. type:"GET",
  4. data:{}, dataType:"json",
  5. timeout:30000,
  6. success:function(data, status, requestCode, response, xhr) {
  7. alert("status:" + status);
  8. alert("result:" + data);
  9. alert("requestCode:" + requestCode);
  10. alert("response:" + JSON.stringify(response));
  11. alert("xhr:" + JSON.stringify(xhr));
  12. }, error:function(xhr,erroType,error,msg) {
  13. alert("xhr:" + JSON.stringify(xhr));
  14. alert("erroType:" + erroType);
  15. alert("error:" + error);
  16. alert("msg:" + msg);
  17. }
  18. });
  19. //获取appcan.cn页面
  20. appcan.request.ajax({
  21. type : 'GET',
  22. url : 'http://appcan.cn',
  23. //添加参数
  24. data : {
  25. name : 'appcan'
  26. },
  27. //期望的返回类型
  28. dataType : 'html',
  29. timeout : 300, //超时时间
  30. success : function(data) {
  31. //获取内容
  32. alert('data');
  33. },
  34. error : function(xhr, type) {
  35. alert('Ajax error!')
  36. },
  37. offline : true
  38. })
  39. //另外一种使用方式
  40. var request = appcan.require('request');
  41. request.ajax({
  42. type : 'POST',
  43. url : 'http://appcan.cn/reg',
  44. data : {
  45. name : 'appcan'
  46. },
  47. contentType : 'application/json',
  48. success : function() {
  49. }
  50. })
  51. //获取appcan.cn页面
  52. appcan.request.ajax({
  53. type : 'GET',
  54. url : 'http://appcan.cn',
  55. //添加参数
  56. data : {
  57. name : 'appcan'
  58. },
  59. //期望的返回类型
  60. dataType : 'html',
  61. timeout : 300, //超时时间
  62. success : function(data) {
  63. //获取内容
  64. alert('data');
  65. },
  66. error : function(xhr, type) {
  67. alert('Ajax error!')
  68. }
  69. })
  70. //例如发送一个post请求,地址为模拟用
  71. request.ajax({
  72. type : 'POST',
  73. url : 'http://appcan.cn/reg',
  74. data : {
  75. name : 'appcan'
  76. },
  77. contentType : 'application/json',
  78. success : function() {
  79. }
  80. })

离线缓存加解密例子:

  1. appcan.ajax({
  2. url : "http://weixin.appcan.cn:8086/test/get",
  3. type : "GET",
  4. data : {
  5. a : 'hello word',
  6. b : 'page'
  7. },
  8. dataType : "json",
  9. timeout : 30000,
  10. offline : true,
  11. crypto : true,
  12. password : "pwd",
  13. success : function(data, status, requestCode, response, xhr) {
  14. alert("status:" + status);
  15. alert("result:" + data);
  16. alert("requestCode:" + requestCode);
  17. alert("response:" + JSON.stringify(response));
  18. alert("xhr:" + JSON.stringify(xhr));
  19. },
  20. error : function(xhr, erroType, error, msg) {
  21. alert("erroType:" + erroType);
  22. alert("error:" + error);
  23. alert("msg:" + msg);
  24. alert("xhr:" + JSON.stringify(xhr));
  25. }
  26. });

appcan.request.get(url,[data],success,[dataType])

发一个http Get请求,这是appcan.request.ajax的简写

  1. url:要请求的地址
  2. data:该参数不是必须的,要传递的参数
  3. success:成功后的回调函数,参考appcan.request.ajax参数中的success
  4. dataType:返回的响应结果的数据类型

例如:

  1. //请求appcan.cn页面的内容
  2. appcan.request.get('http://appcan.cn', function(data, status, xhr) {
  3. //数据内容
  4. console.log(data);
  5. });
  6. //另外一种使用方式
  7. var request = appcan.require('request');
  8. request.get('http://appcan.cn', function(data, status, xhr) {
  9. //数据内容
  10. console.log(data);
  11. });
  12. appcan.request.post(url, [data], success,[dataType])
  13. 发起一个http Post请求
  14. url:要请求的地址
  15. data:要发出的请求的参数
  16. success:请求的成功的回调
  17. dataType:返回的响应结果的数据类型
  18. 例如:
  19. //发送一个简单的post数据到appcan.cn
  20. appcan.request.post('http://appcan.cn', {
  21. name : 'appcan'
  22. }, function(data, status, xhr) {
  23. //打印结果
  24. console.log(data);
  25. });
  26. //另外一种使用方式
  27. var request = appcan.require('request');
  28. request.post('http://appcan.cn', {
  29. name : 'appcan'
  30. }, function(data, status, xhr) {
  31. //打印结果
  32. console.log(data);
  33. });

appcan.request.getJSON(url,[data],success)

发起一个http get请求来获取json数据

  1. url:要获取的json数据的地址
  2. data:要发送请求的参数
  3. success:成功后的回调

例如:

  1. //获取一个json数据
  2. appcan.request.getJSON('http://appcan.cn/a.json', function(data) {
  3. //打印json数据
  4. console.log(data);
  5. });
  6. //另一种使用方式
  7. var request = appcan.require('request');
  8. request.getJSON('http://appcan.cn/a.json', function(data) {
  9. //打印json数据
  10. console.log(data);
  11. });

appcan.request.postForm(selector,success,error)

序列化表单内容并提交表单

  1. selector:表单的css选择器,或者是form元素
  2. success(data):表单提交成功后的回调,data服务器端的返回值
  3. error(err):表单提交失败的时候的回调,err错误对象

例如:

  1. //获取一个json数据
  2. $('form').on('submit', function() {
  3. var form = $('from');
  4. appcan.request.postForm(form);
  5. return false;
  6. });
  7. //另一种使用方式
  8. var request = appcan.require('request');
  9. $('form').on('submit', function() {
  10. var form = $('from');
  11. request.postForm(form);
  12. return false;
  13. });

appcan.request.clearOffline(url,callback,data)

  1. url:需要清除离线数据的url
  2. callback(err,data,dataType,optId) : 执行成功后的回调函数
  3. data:与appcan.request.ajax参数中的data相同

例如:

  1. //清除指定url的离线缓存数据
  2. appcan.request.clearOffline({
  3. url : 'http://weixin.appcan.cn:8086/test/get',
  4. callback : function(err, data, dataType, optId) {
  5. if (err) {
  6. //清除缓存错误
  7. return;
  8. }
  9. if (data == 0) {
  10. //清除缓存成功
  11. } else {
  12. //清除缓存失败
  13. }
  14. }
  15. });

例如:

  1. //清除指定url的离线缓存数据
  2. appcan.request.clearOffline({
  3. url : 'http://weixin.appcan.cn:8086/test/get',
  4. callback : function(err, data, dataType, optId) {
  5. if (err) {
  6. //清除缓存错误
  7. return;
  8. }
  9. if (data == 0) {
  10. //清除缓存成功
  11. } else {
  12. //清除缓存失败
  13. }
  14. },
  15. data:{
  16. a : 'hello word',
  17. b : 'page'
  18. }
  19. });
  20. a
原文地址:https://www.cnblogs.com/lnn-713/p/5353223.html