ajax 返回数据[Object Object]

英文字母打字员 2015-04-15 10:26:16
问 这里面到底出现什么问题,

前台
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Validation.aspx.cs" Inherits="WebCrm.Validation" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<link href="jquery-easyui-1.4/themes/default/easyui.css" rel="stylesheet" />
<link href="jquery-easyui-1.4/themes/icon.css" rel="stylesheet" />
<script src="jquery-easyui-1.4/jquery.min.js" type="text/javascript"></script>
<script src="jquery-easyui-1.4/jquery.easyui.min.js" type="text/javascript"></script>
<link rel="stylesheet" type="text/css" href="Styles/admin-all.css" />

<script type="text/javascript">
function Send() {
var loginUserId = $("#<%=LoginID.ClientID%>").val();
$.ajax({
//提交方式
type: "Post",
//路径
url: "Code/ValidCode.ashx?Method=Send",
data: {
Loginid: loginUserId,
d: new Date()
},
dataType: "text",
//返回数据-需要返回JSON格式的数据(Jquery 1.4版本+以后要求返回数据格式是严格的JSON格式的数据)
success: function (json) {

alert(json);
//if (json.MessageText == 1) {
// alert("发送邮件成功.");
//} else if (json.MessageText == 0) {
// alert("Send mail success.");
//}
},
error: function (e) {
alert(e);
}

});
}


</script>
</head>
<body>
<form id="form1" runat="server">
<asp:HiddenField runat="server" ID="LoginID" />
<div align="center">
<table width="300px;">
<tr>
<td colspan="3">
<asp:Label runat="server" Text="请点击'发送验证码'按钮发送验证码到你的邮箱 " Font-Names="幼圆" Font-Size="Small" ForeColor="Blue"></asp:Label>
</td>
</tr>
<tr>
<td>
<input type="text" id="inpvalidationCode" style="width: 211px;" />
</td>

<td>
<button id="butvalidation" class="black" style="width: 122px; height: 25px;">验证</button>
</td>
<td>
<button id="butSend" class="black" style="width: 131px; height: 25px;" onclick="javascript:Send();">发送验证码</button>
</td>
</tr>

</table>
</div>
</form>
</body>
</html>


ASHX文件:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Web;
using System.Web.Script.Serialization;
using System.Web.SessionState;
using Entity;
using Newtonsoft.Json;

namespace WebCrm.Code
{
/// <summary>
/// ValidCode 的摘要说明
/// </summary>
public class ValidCode : IHttpHandler
{
HttpContext _httpContext;
HttpCookie _cookie;
public void ProcessRequest(HttpContext context)
{
//不让浏览器缓存
context.Response.ContentType = "text/plain";
//context.Response.Buffer = true;
//context.Response.ExpiresAbsolute = DateTime.Now.AddDays(-1);
//context.Response.AddHeader("pragma", "no-cache");
//context.Response.AddHeader("cache-control", "");
//context.Response.CacheControl = "no-cache";
_httpContext = context;
string method = _httpContext.Request["Method"].ToString();
MethodInfo methodInfo = this.GetType().GetMethod(method);
methodInfo.Invoke(this, null);

}

/// <summary>
/// 发送验证码
/// </summary>
public void Send()
{
//Json数据返回格式
Entity.ReturnsData returnsData = new Entity.ReturnsData();
//获取对象值
string login = _httpContext.Request["Loginid"];
//获取当前用户缓存
Entity.LoginInfo loginInfo = BLL.CacheBll.GetCache(login) as Entity.LoginInfo;
if (loginInfo != null)
{
int d = 0;
//判断当前是否存在Cookies
if (_httpContext.Request.Cookies["ValidInfo"] == null)
{
Random rand = new Random();
d = rand.Next(100001, 999999);
//Session["ValidCode"] = d;
TimeSpan ts = new TimeSpan(0, 0, 15, 00);//cookie有效作用时间
_cookie = new HttpCookie("ValidInfo");
_cookie.Expires = DateTime.Now.Add(ts);
_cookie.Values.Add("ValidCode", d.ToString());
_httpContext.Response.AppendCookie(_cookie);
BLL.EmailHelper.SendMail(d, loginInfo.DefaultMail, loginInfo.IsEnglish);
returnsData.MessageText = loginInfo.IsEnglish ? 0 : 1;
_httpContext.Response.Write(returnsData.MessageText);
_httpContext.Response.End();
}
}
}


/// <summary>
/// 验证
/// </summary>
public void Validation()
{

}

public bool IsReusable
{
get
{
return false;
}
}
}
}
...全文
4402 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
正确的解决办法,也在这里说下: 第一版本没有问题,第二我用了Easyui 所以在UI只能够用Easyui的按钮或者控件,不能够使用HTML的控件,所以把按钮换成Easyui的按钮以后,就可以返回值啦。希望能够帮到需要的人
  • 打赏
  • 举报
回复
Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user's experience. For more help, check http://xhr.spec.whatwg.org/. browserLink:37 Setting 'XMLHttpRequest.withCredentials' for synchronous requests is deprecated. 这一段是用Google 原因是什么,这个要怎么解决
  • 打赏
  • 举报
回复
引用 2 楼 starfd 的回复:
if (json== 1) {
alert("发送邮件成功.");
} else {
 alert("Send mail success.");
}
这是个别名吧,也可以叫做Date这样子的
  • 打赏
  • 举报
回复
引用 1 楼 starfd 的回复:
返回了json对象?这不可能啊 你的后台返回的是text/plain,前台ajax请求中也是dataType: "text" 看你就只返回了一个文本,那就直接用string判断就可以了,没必要纠结json
这样子回答你 现在输出的就是一个值 但是前台依然得不到值
  • 打赏
  • 举报
回复
if (json== 1) {
alert("发送邮件成功.");
} else {
 alert("Send mail success.");
}
  • 打赏
  • 举报
回复
返回了json对象?这不可能啊 你的后台返回的是text/plain,前台ajax请求中也是dataType: "text" 看你就只返回了一个文本,那就直接用string判断就可以了,没必要纠结json
又醒着到清晨 2015-04-15
  • 打赏
  • 举报
回复
换一个JQ版本。
mimiooo1003 2015-04-15
  • 打赏
  • 举报
回复
json.ResponseText 试试

62,072

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧