dojo ajax post 和 get 的方法的使用
来源:互联网 发布:遗传算法 matlab 编辑:程序博客网 时间:2024/05/22 23:13
dojo ajax post 的方法的使用
dojo 是非常强大的前端开发平台,对标签、JS 都有着完善的操作机制,以下举例 dojo 向服务器 post 和 get 的实例:
1、数据来自 from 的 dojo post 操作
function sendForm(){ var form = dojo.byId("myform"); dojo.connect(form, "onsubmit", function(event){ // Stop the submit event since we want to control form submission. dojo.stopEvent(event); // The parameters to pass to xhrPost, the form, how to handle it, and the callbacks. // Note that there isn't a url passed. xhrPost will extract the url to call from the form's //'action' attribute. You could also leave off the action attribute and set the url of the xhrPost object // either should work. var xhrArgs = { form: dojo.byId("myform"), handleAs: "text", load: function(data){ dojo.byId("response").innerHTML = "Form posted."; }, error: function(error){ // We'll 404 in the demo, but that's okay. We don't have a 'postIt' service on the // docs server. dojo.byId("response").innerHTML = "Form posted."; } } // Call the asynchronous xhrPost dojo.byId("response").innerHTML = "Form being sent..." var deferred = dojo.xhrPost(xhrArgs); });}dojo.ready(sendForm);
2、数据直接在 js 内构建的 dojo post 操作
function sendText(){ var button = dijit.byId("submitButton2"); dojo.connect(button, "onClick", function(event){ // The parameters to pass to xhrPost, the message, and the url to send it to // Also, how to handle the return and callbacks. var xhrArgs = { url: "postIt", postData: "Some random text", handleAs: "text", load: function(data){ dojo.byId("response2").innerHTML = "Message posted."; }, error: function(error){ // We'll 404 in the demo, but that's okay. We don't have a 'postIt' service on the // docs server. dojo.byId("response2").innerHTML = "Message posted."; } } dojo.byId("response2").innerHTML = "Message being sent..." // Call the asynchronous xhrPost var deferred = dojo.xhrPost(xhrArgs); });}dojo.ready(sendText);
3、数据以 json 方式提交的 dojo post 操作
function sendText(){ var button = dijit.byId("submitButton2"); dojo.connect(button, "onClick", function(event){ // The parameters to pass to xhrPost, the message, and the url to send it to // Also, how to handle the return and callbacks. var xhrArgs = { url: "{{baseUrl}}dojo/dojo.js", postData: dojo.toJson({key1:"value1",key2:{key3:"value2"}}), handleAs: "text", load: function(data){ dojo.byId("response2").innerHTML = "Message posted."; }, error: function(error){ // We'll 404 in the demo, but that's okay. We don't have a 'postIt' service on the // docs server. dojo.byId("response2").innerHTML = "Message posted."; } } dojo.byId("response2").innerHTML = "Message being sent..." // Call the asynchronous xhrPost var deferred = dojo.xhrPost(xhrArgs); });}dojo.ready(sendText);
postData 以 json 或 text 传入,尤其是针对传入参数结构不定的情况,service 获取参数 java 代码如下:
Enumeration<String> enumeration = request.getParameterNames(); while (enumeration.hasMoreElements() == true) { String strParameterName = enumeration.nextElement().toString(); String strValue = request.getParameter(strParameterName); ............ }
4.以参数及变量形式的 dojo 提交
function sendText(); { var xhrArgs = { url : "/service/path", content : { name : 'your name', data1 : 'data1', data2 : 'data2', data3 : 'data3', }, handleAs : "text", // 同步,默认 false sync: true, load : function(data) { console.debug(data); }, error : function(error) { console.debug(data); } } var deferred = dojo.xhrPost(xhrArgs); }
上面全是 post 方式,get 方式几乎一样
dojo.ready(function(){ // Look up the node we'll stick the text under. var targetNode = dojo.byId("licenseContainer"); // The parameters to pass to xhrGet, the url, how to handle it, and the callbacks. var xhrArgs = { url: "{{dataUrl}}dojo/LICENSE", handleAs: "text", load: function(data){ // Replace newlines with nice HTML tags. data = data.replace(/\n/g, "<br>"); // Replace tabs with spaces. data = data.replace(/\t/g, " "); targetNode.innerHTML = data; }, error: function(error){ targetNode.innerHTML = "An unexpected error occurred: " + error; } } // Call the asynchronous xhrGet var deferred = dojo.xhrGet(xhrArgs);});
通过以下命令将字符串 json 转化为对象 json
json = JSON.parse(data, true);
以上参考来自
http://dojotoolkit.org/reference-guide/1.10/dojo/xhrPost.html
https://dojotoolkit.org/reference-guide/1.10/dojo/xhrGet.html
0 0
- dojo ajax post 和 get 的方法的使用
- AJAX的GET和POST方法的使用
- jquery的get,post和ajax方法的使用
- jquery的get,post和ajax方法的使用
- jquery的get,post和ajax方法的使用
- Ajax的Get、Post方法的使用
- Ajax的get和post方法
- Ajax GET 和 POST 的使用
- ajax的get和post
- AJAX的POST和GET
- 使用AJAX 的Get方法与Post方法
- get和post方法的使用
- ajax异步请求的get方法和post方法简单使用
- Ajax发送GET和POST请求的方法
- AJAX中GET和POST方法的区别
- Ajax发关Get和Post请求的方法
- 6.5.2: jQuery的Ajax之$.get()和$.post()方法
- jQuery - AJAX get() 和 post() 方法---非常好的实例
- 用Java代码实现类似Excel单变量求解的功能(Case study-Facebook User Base Growth )
- Javascript算法练习(三)
- ActiveMQ源码解析(五):聊聊activemq的broker集群
- ElasticSearch源码解析(一):转一篇介绍中文分词的文章
- Writing a Linux device driver module for kernels 2.6 or later with udev
- dojo ajax post 和 get 的方法的使用
- Android之ActivityManager与Proxy模式的运用
- :在scrapy的callback中使用lambda小结
- Linux常用命令-curl
- 关于scrapy有用的链接
- C# SOCKET GET/POST的实现
- 学年总结(2015-2016学年回顾)
- Mac的固件密码能被破解吗?
- 跟我学Android之二 第一个程序