简体   繁体   English

使用JSON访问Web服务引用未找到任何Web服务

[英]Accessing webservice reference with JSON gives no webservice found

I've searched but couldn't find a solution yet. 我已经搜索过,但找不到解决方案。

When I want to access my web service "costService.asmx" with JSON, client side to update an asp:chart in real time, I get an error from JSON saying that the web service "costService" could not be found. 当我想使用JSON访问Web服务“ costService.asmx”时,客户端要实时更新asp:chart,我从JSON中收到一条错误消息,提示找不到Web服务“ costService”。

I've tried almost everything: I followed this: http://encosia.com/using-jquery-to-consume-aspnet-json-web-services/ 我已经尝试了几乎所有内容:我遵循了以下步骤: http//encosia.com/using-jquery-to-consume-aspnet-json-web-services/

That code works but when I use it with my own service it doesn't work. 该代码有效,但是当我将其与自己的服务一起使用时,它将无法正常工作。

I've added 我已经添加

<webServices>
    <protocols>
        <add name="HttpSoap"/>
        <add name="HttpPost"/>
        <add name="HttpGet"/>
        <add name="Documentation"/>
        <add name="HttpPostLocalhost"/>
    </protocols>
</webServices>

I'm almost certain it's because I have separated my service and site as two projects. 我几乎可以肯定这是因为我将服务和站点划分为两个项目。 My site has a srevice reference called "CostServiceProxy" and using that server side on my site works without a problem. 我的站点有一个名为“ CostServiceProxy”的服务参考,并且在我的站点上使用该服务器端可以正常工作。

so how do I make JSON use "CostServiceProxy" ie my service reference? 那么如何使JSON使用“ CostServiceProxy”(即我的服务引用)?

the client side code: 客户端代码:

$(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); }
        });
    }

Server side I do: 服务器端我这样做:

CostServiceProxy.CostServiceSoapClient client = new CostServiceProxy.CostServiceSoapClient();

To use the methodes and objects from the service. 使用服务中的方法和对象。

Edit 编辑

My WebService class: 我的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 {

My methode in that class: 我在那个班上的方法:

[WebMethod]
public String DrawChart(Int32 iType, double CostToRender) {

When I look under the Net tab of firebig I see the following: 当我在firebig的“网络”选项卡下查看时,会看到以下内容: 500内部服务器错误POST DrawChart Edit II 编辑II

I've gotten it to work on my site by putting the methode on my default.aspx code behind. 通过将methode放在我的default.aspx代码后面,我可以在我的网站上使用它。 It still has some imperfections, but the chart with the data gets shown. 它仍然有一些缺陷,但是显示了带有数据的图表。 I will now try and see if it works when I change the url to that of the webservice. 现在,当我将URL更改为Web服务的URL时,我将尝试查看它是否有效。

If you haven't modified your service at all, the first thing you'll need to do is make sure that the service class is decorated with the [ScriptService] attribute. 如果您根本没有修改服务,那么您要做的第一件事就是确保服务类装饰有[ScriptService]属性。 That's what enables it to respond with JSON if you POST to it with a Content-Type of application/json . 如果您使用Content / Type的application/json POST到JSON,这就是它能够响应JSON的原因。 Without that attribute, you'll only get XML out (and it won't know how to interpret that JSON parameter string you're POSTing in either). 没有该属性,您将只能获得XML(并且它不知道如何解释您要在其中发布的JSON参数字符串)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM