1.借助jquery-jsonp插件
$.jsonp({
url: url,
data: {
'name': usd,
'passwd': pass
},
callbackParameter: "callback",
success: function(newImgSrc, textStatus, xOptions) {
alert(444);
console.log(xOptions.data.imgSrc);
},
error: function(xOptions, textStatus) {}
});
后台代码:
Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss")
.create(); String ster="result:"+gson.toJson(result);
return "callbackJieduanList({'message':'获取成功','state':'1',"+ster+"})";
callbackJieduanList 为html前端的js方法
2.hbuilder
ref:http://www.cnblogs.com/sjd1118/p/5785067.html
<div id="a1" onclick="testXHR()" style="font-size: 5em;">sss1</div> <script type="text/javascript">
var url = 'http://192.168.0.122/lxty/index.php/index/index/testajax';
var data;
var xhr=null;
function testXHR(){
if(xhr){
return;
}
xhr=new plus.net.XMLHttpRequest();
xhr.onreadystatechange=xhrStatechange;
xhr.open( "GET", url);
xhr.send();
}
function xhrStatechange(){
if (xhr.readyState==4&&xhr.status == 200) { data = JSON.parse(xhr.responseText); //alert(data.a1)
for(i in data){alert(data[i])} }
} </script>
<
div
id
=
"a1"
onclick
=
"testXHR()"
style
=
"font-size: 5em;"
>sss1</
div
>
<
script
type
=
"text/javascript"
>
var data;
var xhr=null;
function testXHR(){
if(xhr){
return;
}
xhr=new plus.net.XMLHttpRequest();
xhr.onreadystatechange=xhrStatechange;
xhr.open( "GET", url);
xhr.send();
}
function xhrStatechange(){
if (xhr.readyState==4&&xhr.status == 200) {
data = JSON.parse(xhr.responseText);
//alert(data.a1)
for(i in data){alert(data[i])}
}
}
</
script
>