gpt4 book ai didi

javascript - 在 javascript 中调用我的 wcf 服务时出现 404 - 我缺少什么?

转载 作者:行者123 更新时间:2023-12-02 18:23:20 24 4
gpt4 key购买 nike

我正在尝试调用我的 WCF 服务,并且我正在使用 jQuery 的 AJAX。除非我弄错了,否则问题似乎出在我尝试以 JSON 格式获取数据时。我不确定 URL 是否正确。

这是我的 service.svc.cs。我想调用AddNewQuery

//------------------------------------------------------------------------------
// <copyright file="WebDataService.svc.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
//------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Data.Services;
using System.Data.Services.Common;
using System.Linq;
using System.ServiceModel.Web;
using System.Web;
using System.Net;
using System.Data.Objects;
using System.Data.Entity;
using System.Net.Http;
using System.Web.Http;
using System.Web;
using System.ServiceModel.Web;

namespace BRADAPI
{
[JSONPSupportBehavior]
[System.ServiceModel.ServiceBehavior(IncludeExceptionDetailInFaults = true)]
public class Service1 : System.Data.Services.DataService<BradOnlineEntities>
{
// This method is called only once to initialize service-wide policies.
public static void InitializeService(DataServiceConfiguration config)
{
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*");
config.SetEntitySetAccessRule("*", EntitySetRights.All);
config.UseVerboseErrors = true;
config.SetServiceOperationAccessRule("AddNewQuery", ServiceOperationRights.All);
config.SetServiceOperationAccessRule("GetQueryByID", ServiceOperationRights.All);
config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V3;
}

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.ServerAndPrivate);
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<tblContactQuery> GetQueryByID(Guid QueryID)
{
IQueryable<tblContactQuery> biglist = (from c in this.CurrentDataSource.tblContactQueries where c.QueryID.Equals(QueryID) select c);
return biglist;
}

[WebGet]
public IQueryable<tblContactQuery> AddNewQuery(string QueryText, string UserID)
{
// Make NULL to remove compile errors
Guid GUserID = Guid.Parse(UserID);
Guid QueryID = Guid.NewGuid();

tblContactQuery C = new tblContactQuery
{
QueryID = QueryID,
UserID = GUserID,
QueryText = QueryText,
};
try
{
this.CurrentDataSource.tblContactQueries.Add(C);
this.CurrentDataSource.SaveChanges();
return GetQueryByID(QueryID);

}
catch
{
return GetQueryByID(QueryID);
}
}

}
}

Calling service directly in the browser

Calling Service in my JS - I am getting 404

我的 JS 代码

function sendQuery() {

userId = $("#hdnUserId").val();
contactId = $("#hdnContactId").val();
txtQuery = $('#txtQuery').val();

var successFlag;

$.ajax({
url: url,
data: "UserID='" + userId + "'&" + "QueryText='" + txtQuery + "'&" + "ContactID='" + contactId + "'",
type: "GET",
async: false,
datatype: "json",
success: function (data) {
successFlag = 1;
},
error: function (data) {
successFlag == 0;
}
});

if (successFlag == 1) {
alert("Thank you, your query has been sent to a member of the Alf team");
$("#dialog").css("display", "false");
$("#dialog").dialog("close");
}

else if (successFlag == 0) {
alert("Query not sent to the ALF team");
$("#dialog").css("display", "false");
$("#dialog").dialog("close");
}
}

我的成功和错误 block 没有受到影响,因为它没有影响服务。所以这不是成功或失败。

更改我的网址有什么建议吗?

最佳答案

URL 值参数不由 ' ' 分隔。使用 encodeURIComponent(...) 正确转义参数。

而不是

data: "UserID='" + userId + "'&" + "QueryText='" + txtQuery + "'&" + "ContactID='" + contactId + "'"

尝试

data: "UserID=" + userId + "&QueryText=" + encodeURIComponent(txtQuery) + "&ContactID=" + encodeURIComponent(contactId),

此外,根据您的一张屏幕截图,您的网址似乎有误。在 /image/KtFlv.png网址为http://stating1......com/alfapi/Service.svc/AddNewQuery?format=json?QueryText

这表明您的 url 变量已包含查询,即 http://stating1......com/alfapi/Service.svc/AddNewQuery?format=json (?format=json 部分是 url 参数中的查询,在使用 $.ajax(...) 中的 data 时应避免使用该查询.

改为

var url = 'http://stating1......com/alfapi/Service.svc/AddNewQuery';

....

$.ajax({
url: url,
// move format=json from your url variable to data: part
data: "format=json&UserID=" + encodeURIComponent(userId) + "&QueryText=" + encodeURIComponent(txtQuery) + "&ContactID=" + encodeURIComponent(contactId),
type: "GET",
async: false,
datatype: "json",
success: function (data) {
successFlag = 1;
},
error: function (data) {
successFlag == 0;
}
});

参见"JavaScript encodeURIComponent() Function"

关于javascript - 在 javascript 中调用我的 wcf 服务时出现 404 - 我缺少什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18616170/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com