我有一个本地托管的 WCF 服务和一个本地托管的网站。我对成功处理的 JSON 数据发出 GET ajax 请求。我有一个问题,在 Chrome 和 Firefox 中它进入了 Ajax 请求的错误块。
<!--jQuery dependencies-->
<link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.9.1/themes/base/jquery-ui.css" />
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.9.1/jquery-ui.min.js"></script>
<script type="text/javascript" src="../../Library/JS/pqgrid.min.js"></script>
<script type="text/javascript">
var jqNew = $.noConflict(true);
</script>
由于网站的复杂性,我使用了两个不同版本的 JQuery。较新版本的别名为 jqNew。
jqNew(document).ready(function () {
$($.ajax({
url: "http://wks52025:82/WcfDataService.svc/GetNotesFromView()?$format=json",
type: "GET",
datatype: "json",
async: false,
success: function (data, textStatus) {
alert(textStatus);
$.each(data.d, function (i, item) {
alert(i);
DataArray[i] = [];
DataArray[i][0] = item.NotesID;
DataArray[i][1] = item.NotesTitle;
DataArray[i][2] = item.NotesText;
DataArray[i][3] = item.ProfileName;
DataArray[i][4] = item.IsShared;
DataArray[i][5] = item.NameOfUser;
});
},
error: function (data, textStatus) {
alert(textStatus);
}
})).then(buildGrid(DataArray));
这是我的要求。它是同步的,因为我在构建网格之前需要数据。这是一个 GET 请求,因为我想要的只是检索数据。我没有做跨域请求,因为一切都在本地发生,至少我认为它是本地的,因为网站和服务是本地主机。
//------------------------------------------------------------------------------
// <copyright file="WebDataService.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
//------------------------------------------------------------------------------
using System;
using System.Data.Services;
using System.Data.Services.Common;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel.Web;
using System.Web;
using System.Data.Services.Parsing;
using WCFNotes;
[JSONPSupportBehavior]
public class WcfDataService : DataService< GenesisOnlineEntities >
{
// This method is called only once to initialize service-wide policies.
public static void InitializeService(DataServiceConfiguration config)
{
// TODO: set rules to indicate which entity sets and service operations are visible, updatable, etc.
// Examples:
config.UseVerboseErrors = true;
config.SetEntitySetAccessRule("*", EntitySetRights.All);
config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V3;
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*");
config.SetServiceOperationAccessRule("GetNotes", ServiceOperationRights.AllRead);
config.SetServiceOperationAccessRule("GetNotesFromView", ServiceOperationRights.AllRead);
config.SetServiceOperationAccessRule("AddNewNote", ServiceOperationRights.All);
config.SetServiceOperationAccessRule("UpdateNote", ServiceOperationRights.All);
config.SetServiceOperationAccessRule("DeleteNote", ServiceOperationRights.All);
}
protected override void OnStartProcessingRequest(ProcessRequestArgs args)
{
base.OnStartProcessingRequest(args);
//Cache for a minute based on querystring
//HttpContext context = HttpContext.Current;
HttpCachePolicy c = HttpContext.Current.Response.Cache;
c.SetCacheability(HttpCacheability.NoCache);
//c.SetExpires(HttpContext.Current.Timestamp.AddSeconds(60));
c.VaryByHeaders["Accept"] = true;
c.VaryByHeaders["Accept-Charset"] = true;
c.VaryByHeaders["Accept-Encoding"] = true;
c.VaryByParams["*"] = true;
}
[WebGet]
public IQueryable<tblNote> GetNotes()
{
IQueryable<tblNote> biglist = (from c in this.CurrentDataSource.tblNotes select c);
return biglist;
}
[WebGet]
public IQueryable<vw_Note> GetNoteByID(Guid NoteID)
{
IQueryable<vw_Note> biglist = (from c in this.CurrentDataSource.vw_Notes where c.NotesID.Equals(NoteID) select c);
return biglist;
}
[WebGet]
public IQueryable<vw_Note> GetNotesFromView()
{
Guid UserID = new Guid("8b0e303a-68aa-49a5-af95-d994e2bdd5ac");
IQueryable<vw_Note> biglist = (from c in this.CurrentDataSource.vw_Notes select c);
return biglist;
}
//[WebGet]
//public void AddNewNote(string ParamNoteTitle, string ParamNoteText)
//{
// //My hardcoded values for now...
// int ParentID = 8879;
// int JobID = 1000088150;
// int ContactID = 309;
// Guid UserID = Guid.NewGuid();
// string RelatedType = "Advertiser Contact";
// bool IsShared = true;
// tblNote N = new tblNote
// {
// NotesTitle = ParamNoteTitle,
// NotesText = ParamNoteText,
// ParentID = ParentID,
// ContactID = ContactID,
// JobID = JobID,
// UserID = UserID,
// GroupID = null,
// RelatedType = RelatedType,
// IsShared = IsShared
// };
// try
// {
// this.CurrentDataSource.tblNotes.Add(N);
// this.CurrentDataSource.SaveChanges();
// }
// catch (Exception ex)
// {
// }
//}
[WebGet]
public IQueryable<vw_Note> AddNewNote(string ParamNoteTitle, string ParamNoteText)
{
//My hardcoded values for now...
int ParentID = 8879;
int JobID = 1000088150;
int ContactID = 309;
Guid UserID = new Guid("8b0e303a-68aa-49a5-af95-d994e2bdd5ac");
Guid NoteID = Guid.NewGuid();
string RelatedType = "Advertiser Contact";
bool IsShared = true;
tblNote N = new tblNote
{
NotesID = NoteID,
NotesTitle = ParamNoteTitle,
NotesText = ParamNoteText,
ParentID = ParentID,
ContactID = ContactID,
JobID = JobID,
UserID = UserID,
GroupID = null,
RelatedType = RelatedType,
IsShared = IsShared
};
try
{
this.CurrentDataSource.tblNotes.Add(N);
this.CurrentDataSource.SaveChanges();
return GetNoteByID(NoteID);
}
catch (Exception ex)
{
return GetNoteByID(NoteID);
}
}
[WebGet]
public IQueryable<vw_Note> UpdateNote(string NoteID, string ParamNoteTitle, string ParamNoteText)
{
//My hardcoded values for now...
Guid GNoteID = Guid.Parse(NoteID);
try
{
tblNote n = CurrentDataSource.tblNotes.First(i => i.NotesID == GNoteID);
n.NotesTitle = ParamNoteTitle;
n.NotesText = ParamNoteText;
this.CurrentDataSource.SaveChanges();
return GetNoteByID(GNoteID);
}
catch (Exception ex)
{
return GetNoteByID(GNoteID);
}
}
[WebGet]
public IQueryable<vw_Note> DeleteNote(string NoteID)
{
Guid GNoteID = Guid.Parse(NoteID);
tblNote N = new tblNote
{
NotesID = GNoteID,
};
try
{
this.CurrentDataSource.tblNotes.Attach(N);
this.CurrentDataSource.tblNotes.Remove(N);
this.CurrentDataSource.SaveChanges();
// IF doesn't return value, delete was successful
return GetNoteByID(GNoteID);
}
catch (Exception ex)
{
return GetNoteByID(GNoteID);
}
}
}
这是我的 WCF 服务。配置位于代码块的顶部。
任何人都知道为什么这在 Firefox 和 chrome 中不起作用?