求帮助,getJSON.get 回调函数不执行行的问题

网页设计教程与开发
提供各种常见网页效果
提供各种各样的设计教程
装扮QQ,让QQ变得更酷
设计参考,提高自升水平
学习服务器和操作系统
提供各种素材和工具
收藏学习资料
您现在的位置:&&>>&&>>&&>>&&>>&正文
jQuery Ajax Post 回调函数不执行问题的解决方法
今天在写一个检查用户名的功能时,使用的是jQuery.post( url, [data], [callback], [type] )这个函数,但是发现其中的回调函数不能执行。
先来看看我的代码:
前台代码:
&script type="text/javascript"&
function checkUser() {
var user = $('#&%=txtUser.ClientID %&').val();
$.post('checkUser.ashx', { Action: "Check", Name: user },
function (data) {
alert(data);
后台代码(checkUser.ashx):
public void ProcessRequest(HttpContext context)
context.Response.ContentType = "application/json";
string action = context.Request["Action"];
string user = context.Request["Name"];
string result = "{result: 0}";
switch (action)
case "Check":
if (us.isExists(user))
result = "{result: 1}";
context.Response.Write(result);
但是怎么也不执行回调函数中的alert(data)语句,经过单步跟踪,最后发现是返回的Json字符串有问题。
原来jquery中规定返回的JSON字符串的KEY要用引号括起来,如{"result": 1}这样才可以。
下面再看下jquery Ajax 不执行回调函数success的原因
jquery Ajax 不执行回调函数success的原因:
type: "post",
contentType: "application/json",
url: basePath+"pages/Tongji/disposeAgree.action?cepingitem="+cepingitem+"&userrelation="+userrelation,
//data: {fenshu:8},
dataType: "json",
success: function (result) {
alert("操作成功");
error:function(response){
alert("error");
如上,因为设置了属性dataType:"json",故如果后台返回的不是正确的JSON字符串,那么永远不执行回调函数success,而只执行error函数。要么返回正确的JSON字符串,要么把dataType属性改为text
正确的返回JSON字符串如下:
pw.write("{\"name\":\"zs\"}");
pw.write("{\"name\":"+true+"}");
错误的例子:
pw.write("{‘name':‘zs'}");
以上所述是小编给大家介绍的jQuery Ajax Post 回调函数不执行问题的解决方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家的支持!
转载请注明:破洛洛(谢谢合作)
上一篇文章: 下一篇文章:
网友评论:
[][][][][][][][][][](MyheartForever)
(oversky2003)
第三方登录:温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!&&|&&
LOFTER精选
网易考拉推荐
用微信&&“扫一扫”
将文章分享到朋友圈。
用易信&&“扫一扫”
将文章分享到朋友圈。
function changeTomcatStatus(v,ip,port){ $.getJSON("http://xxx/send.jsp?callback=?", {
port:port },
function(data){
alert(data.res); });}调用的JSP端(即上http://xxx/send.jsp)&%@ page language="java" contentType="text/ charset=utf-8"
pageEncoding="utf-8"%&&%String v = (String)request.getParameter("v");String ip = (String)request.getParameter("ip");String port = (String)request.getParameter("port");String checkurl = (String)request.getParameter("checkurl");String callback = (String)request.getParameter("callback");
JNDIService.sendData(v,ip,Integer.parseInt(port),checkurl);response.setContentType("application/json");//输出格式、一定要记得。
response.setCharacterEncoding("utf-8");//设置输出解码方式
out.println(callback + "({res:'ok'})");
%&要想正确执行回调函数,此处的返回格式必须正确
阅读(1493)|
用微信&&“扫一扫”
将文章分享到朋友圈。
用易信&&“扫一扫”
将文章分享到朋友圈。
历史上的今天
在LOFTER的更多文章
loftPermalink:'',
id:'fks_081064',
blogTitle:'getJSON跨域访问及回调函数不执行问题',
blogAbstract:'使用getJSON执行跨域访问时,需要在地址后添加callback=?JS端function changeTomcatStatus(v,ip,port){\t$.getJSON(\"http://xxx/send.jsp?callback=?\",\t{\t\tv:v,\t\tip:ip,\t\tport:port\t},\t\tfunction(data){\t\t
alert(data.res);\t});}调用的JSP端(即上http://xxx/send.jsp)',
blogTag:'',
blogUrl:'blog/static/',
isPublished:1,
istop:false,
modifyTime:7,
publishTime:2,
permalink:'blog/static/',
commentCount:0,
mainCommentCount:0,
recommendCount:0,
bsrk:-100,
publisherId:0,
recomBlogHome:false,
currentRecomBlog:false,
attachmentsFileIds:[],
groupInfo:{},
friendstatus:'none',
followstatus:'unFollow',
pubSucc:'',
visitorProvince:'',
visitorCity:'',
visitorNewUser:false,
postAddInfo:{},
mset:'000',
remindgoodnightblog:false,
isBlackVisitor:false,
isShowYodaoAd:false,
hostIntro:'',
hmcon:'1',
selfRecomBlogCount:'0',
lofter_single:''
{list a as x}
{if x.moveFrom=='wap'}
{elseif x.moveFrom=='iphone'}
{elseif x.moveFrom=='android'}
{elseif x.moveFrom=='mobile'}
${a.selfIntro|escape}{if great260}${suplement}{/if}
{list a as x}
推荐过这篇日志的人:
{list a as x}
{if !!b&&b.length>0}
他们还推荐了:
{list b as y}
转载记录:
{list d as x}
{list a as x}
{list a as x}
{list a as x}
{list a as x}
{if x_index>4}{break}{/if}
${fn2(x.publishTime,'yyyy-MM-dd HH:mm:ss')}
{list a as x}
{if !!(blogDetail.preBlogPermalink)}
{if !!(blogDetail.nextBlogPermalink)}
{list a as x}
{if defined('newslist')&&newslist.length>0}
{list newslist as x}
{if x_index>7}{break}{/if}
{list a as x}
{var first_option =}
{list x.voteDetailList as voteToOption}
{if voteToOption==1}
{if first_option==false},{/if}&&“${b[voteToOption_index]}”&&
{if (x.role!="-1") },“我是${c[x.role]}”&&{/if}
&&&&&&&&${fn1(x.voteTime)}
{if x.userName==''}{/if}
网易公司版权所有&&
{list x.l as y}
{if defined('wl')}
{list wl as x}{/list}

我要回帖

更多关于 getjson 怎么回调 的文章

 

随机推荐