我正在使用jQuery 在Ajax上进行第一次破解.我将数据放到我的页面上,但是我在为Date数据类型返回的JSON数据方面遇到了一些麻烦.基本上,我得到的字符串看起来像这样:
/Date(1224043200000)/
Run Code Online (Sandbox Code Playgroud)
从全新的人到JSON - 如何将其格式化为短日期格式?这应该在jQuery代码中的某个地方处理吗?我试过使用jQuery.UI.datepicker插件$.datepicker.formatDate()没有任何成功.
仅供参考:以下是我提出的解决方案:
function getMismatch(id) {
$.getJSON("Main.aspx?Callback=GetMismatch",
{ MismatchId: id },
function (result) {
$("#AuthMerchId").text(result.AuthorizationMerchantId);
$("#SttlMerchId").text(result.SettlementMerchantId);
$("#CreateDate").text(formatJSONDate(Date(result.AppendDts)));
$("#ExpireDate").text(formatJSONDate(Date(result.ExpiresDts)));
$("#LastUpdate").text(formatJSONDate(Date(result.LastUpdateDts)));
$("#LastUpdatedBy").text(result.LastUpdateNt);
$("#ProcessIn").text(result.ProcessIn);
}
);
return false;
}
function formatJSONDate(jsonDate) {
var newDate = dateFormat(jsonDate, "mm/dd/yyyy");
return newDate;
}
Run Code Online (Sandbox Code Playgroud)
此解决方案从回调方法获取我的对象,并使用日期格式库正确显示页面上的日期.
我在jQuery的ajax调用中遇到错误.
这是我的jQuery函数:
function DeleteItem(RecordId, UId, XmlName, ItemType, UserProfileId) {
var obj = {
RecordId: RecordId,
UserId: UId,
UserProfileId: UserProfileId,
ItemType: ItemType,
FileName: XmlName
};
var json = Sys.Serialization.JavaScriptSerializer.serialize(obj);
$.ajax({
type: "POST",
url: "EditUserProfile.aspx/DeleteRecord",
data: json,
contentType: "application/json; charset=utf-8",
dataType: "json",
async: true,
cache: false,
success: function(msg) {
if (msg.d != null) {
RefreshData(ItemType, msg.d);
}
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
alert("error occured during deleting");
}
});
}
Run Code Online (Sandbox Code Playgroud)
这是我的WebMethod:
[WebMethod]
public static string DeleteRecord(Int64 RecordId, Int64 UserId, Int64 …Run Code Online (Sandbox Code Playgroud) 我正在使用.Net framework 2.0/jQuery来对2.0 Web服务进行Ajax调用.无论我在ajax调用中将contentType设置为什么,服务始终返回XML.我想让它回归Json!
这是电话:
$(document).ready(function() {
$.ajax({
type: "POST",
url: "DonationsService.asmx/GetDate",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg) {
// Hide the fake progress indicator graphic.
$('#RSSContent').removeClass('loading');
// Insert the returned HTML into the <div>.
$('#RSSContent').html(msg.d);
}
});
});
Run Code Online (Sandbox Code Playgroud)
以下是Fiddler中请求标头的样子:
POST /DonationsService.asmx/GetDate HTTP/1.1
x-requested-with: XMLHttpRequest
Accept-Language: en-us
Referer: http://localhost:1238/text.htm
Accept: application/json, text/javascript, */*
Content-Type: application/json; charset=utf-8
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; eMusic DLM/4; .NET CLR 2.0.50727)
Host: …Run Code Online (Sandbox Code Playgroud) 我正在使用SharePoint 2010 REST API,它可以以xml或JSON格式返回数据.对于我的场景,我需要JSON.
使用jQuery一切正常:
$.ajax({
type:"GET",
url:url,
dataType:"json",
success: function(data, textStatus, jqXHR){...}
});
Run Code Online (Sandbox Code Playgroud)
但我无法在纯JavaScript中获取JSON,数据以xml形式返回.我错过了什么?
var XHR=new XMLHttpRequest();
XHR.open("GET", url, true);
XHR.setRequestHeader("Content-Type","application/json");
XHR.onreadystatechange = function () {
if (XHR.readyState == 4 && XHR.status == 200) {...}};
XHR.send(null);
Run Code Online (Sandbox Code Playgroud)