我已经搜索过但找不到解决方案。
当我想使用JSON访问我的Web服务“costService.asmx”时,客户端实时更新asp:图表,我从JSON收到错误,说无法找到Web服务“costService”。
我几乎尝试过所有事情: 我跟着这个: http://encosia.com/using-jquery-to-consume-aspnet-json-web-services/
该代码有效,但当我将其用于我自己的服务时,它不起作用。
我添加了
<webServices>
<protocols>
<add name="HttpSoap"/>
<add name="HttpPost"/>
<add name="HttpGet"/>
<add name="Documentation"/>
<add name="HttpPostLocalhost"/>
</protocols>
</webServices>
我几乎可以肯定这是因为我把我的服务和网站分成了两个项目。我的网站有一个名为“CostServiceProxy”的服务引用,并且在我的网站上使用该服务器端工作没有问题。
那么如何让JSON使用“CostServiceProxy”即我的服务参考?
客户端代码:
$(function () {
//intercept the onchange event fire by element
//with "graphType" ID (SELECT)
$(".ddlChartType").change(function () {
//get the attribute "value" of the OPTION
//element selected and pass it as parameter
getChartImage($("option:selected", this).attr("value"));
});
});
function getChartImage(type) {
if (type < 0) return;
//create the object for passing data to Web Service
var dataPassed = new Object();
dataPassed.iType = type;
//call a Web service with jQuery
$.ajax({
type: "POST",
url: WebServiceURL + "/DrawChart",
data: $.toJSON(dataPassed),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) { var data = $.evalJSON(msg).d; $("#ChartArea").attr("src", data); },
error: function (XMLHttpRequest, textStatus, errorThrown) { alert(XMLHttpRequest.responseText); }
});
}
服务器端我做:
CostServiceProxy.CostServiceSoapClient client = new CostServiceProxy.CostServiceSoapClient();
使用服务中的方法和对象。
修改
我的WebService类:
namespace CostService {
/// <summary>
/// Summary description for CostService
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
//To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
[System.Web.Script.Services.ScriptService]
public class CostService : System.Web.Services.WebService {
我班上的方法:
[WebMethod]
public String DrawChart(Int32 iType, double CostToRender) {
当我查看firebig的Net标签下时,我看到以下内容:
编辑II
我已经将我的默认.aspx代码放在我的网站上了。它仍有一些不完善之处,但显示了包含数据的图表。 我现在将尝试查看当我将网址更改为网络服务时是否有效。
答案 0 :(得分:0)
如果您根本没有修改过您的服务,首先需要确保使用[ScriptService]
属性修饰服务类。如果您使用内容类型application/json
对其进行POST,则可以使用JSON进行响应。如果没有该属性,您将只获取XML(并且它将不知道如何解释您正在POST的JSON参数字符串)。