Rob*_*ffe 5 .net c# ajax response umbraco
我有一个.NET方法,为我的数据库添加一个新成员.它通过AJAX请求完成此操作.我有这个工作正常,但我在返回正确的响应消息时遇到问题,因此我可以向用户打印正确的消息.
我的方法目前看起来像这样:
public static string MemberRegister(int process)
{
//here we find form values posted to the current page
HttpRequest post = HttpContext.Current.Request;
//get values from ajax URL
var name = post["name"];
var email = post["email"];
var username = post["username"];
var password = post["password"];
//check if email exists
if (Member.GetMemberFromEmail(email) == null)
{
MemberType userMemberType = new MemberType(1111); //id of membertype 'demo'
Member newMember = Member.MakeNew(name, userMemberType, new umbraco.BusinessLogic.User(0));
newMember.AddGroup(MemberGroup.GetByName("Active").Id);
newMember.Email = email;
newMember.Password = password;
newMember.LoginName = username;
newMember.Save();
return "success";
}
else
{
return "emailError";
}
}
Run Code Online (Sandbox Code Playgroud)
我的Ajax代码如下:
// submit
$registerForm.submit(function() {
$loader.show();
jQuery.ajax({
url: "/processform.aspx",
type: "POST",
data: $(this).serialize()
}).complete(function( response ) {
alert(response.responseText);
if( response.responseText === "success" ) {
$registerSuccess.fadeIn();
} elseif( response.responseText === "emailError" ) {
$registerEmailError.fadeIn();
} else {
$registerError.slideDown();
}
$loader.hide();
});
return false;
});
Run Code Online (Sandbox Code Playgroud)
例如,如果所有成员都存在,则返回响应:
<value>emailError</value>
Run Code Online (Sandbox Code Playgroud)
我只是想让它返回emailError而不是值标签.我该怎么做呢?
提琴手(原始):
HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Vary: Accept-Encoding
Server: Microsoft-IIS/7.5
X-Powered-By: ASP.NET
Date: Sun, 25 Mar 2012 20:59:54 GMT
Content-Length: 25
<value>emailError</value>
POST http://domain.com/base/Forms/MemberRegister/process.aspx HTTP/1.1
Host: domain.com
Connection: keep-alive
Content-Length: 125
Origin: http://domain.com
X-Requested-With: XMLHttpRequest
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.79 Safari/535.11
Content-Type: application/json; charset=UTF-8
Accept: application/json, text/javascript, */*; q=0.01
Referer: http://domain/register.aspx
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-GB,en-US;q=0.8,en;q=0.6
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3
Cookie: ASP.NET_SessionId=ys4mmhsn2mpqcpja1iyjg04m; UMB_UPDCHK=1; __utma=256732567.15732944.1331581910.1332617890.1332627641.11; __utmc=256732567; __utmz=256732567.1331581910.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); UMB_UCONTEXT=12621b40-16fe-4422-a027-cf4fa68fe03d; __utma=176230262.1311679778.1332368941.1332694687.1332708163.12; __utmb=176230262.3.10.1332708163; __utmc=176230262; __utmz=176230262.1332368941.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none)
__VIEWSTATE=%2FwEPDwUENTM4MWRkEytUM47m6NUA6dpfOudOh9t51j6okfG%2BQhu4Em%2B26KU%3D&name=ppp&email=ppp&username=ppp&username=ppp
Run Code Online (Sandbox Code Playgroud)
谢谢罗伯特
以下是如何使用Umbraco Base返回JSON对象的一个很好的示例:
/* Be sure to add References to:
*
* umbraco.dll
* System.Web.dll
* System.Web.Extensions.dll
*/
using System.Web;
using System.Web.Script.Serialization;
using umbraco.presentation.umbracobase;
namespace CoB.Umb.Base.Example
{
[RestExtension("Example")]
public class Example
{
[RestExtensionMethod(returnXml = false, allowAll = true)]
public static void Get()
{
string json = "";
var person = new
{
firstName = "John",
lastName = "Doe"
};
json = new JavaScriptSerializer().Serialize(person);
HttpContext.Current.Response.ContentType = "application/json";
HttpContext.Current.Response.Write(json);
}
}
}
Run Code Online (Sandbox Code Playgroud)
和javascript:
$.getJSON('/base/Example/Get', function (data) {
alert("Hey, " + data.firstName + " " + data.lastName);
});
Run Code Online (Sandbox Code Playgroud)
您可能需要装饰该方法以返回 Json
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public static string MemberRegister(int process)
complete(function( response ) {
if (response.d == "success") {
//
}
}
Run Code Online (Sandbox Code Playgroud)
编辑
jQuery.ajax({
url: "/processform.aspx",
type: "POST",
contentType: "application/json; charset=utf-8",
dataType: "json",
data: $(this).serialize()
})..
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5214 次 |
| 最近记录: |