从调用$ JQuery的阿贾克斯一个WebService得到错误500 [英] Calling a WebService from JQuery $.ajax gets Error 500

查看:138
本文介绍了从调用$ JQuery的阿贾克斯一个WebService得到错误500的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我和我的第一个WebService的挣扎,所以我决定剥离下来的一切的基本知识。仍然没有工作。

我创建了一个全新的ASP.NET网站项目。

我添加了一个名为WebService的文件夹。在WebService的文件夹中,我添加使用ASP.NET模板创建一个新的WebService。我修改模板只能取消注释[System.Web.Script.Services.ScriptService]

在Default.aspx页,欢迎使用ASP.NET之后,我添加了一个面板如下:

 < ASP:面板ID =Panel1的=服务器HEIGHT =200像素WIDTH =200像素背景色=蓝的onclick =的HelloWorld(); >
< / ASP:面板>

我还增加了以下脚本到HeaderContent区:

 <脚本类型=文/ JavaScript的语言=使用JavascriptSRC =htt​​p://ajax.googleapis.com/ajax/libs/jquery/1.6.2/ jquery.min.js>< / SCRIPT><脚本类型=文/ JavaScript的>    功能OnSuccessCall(响应){
        警报(成功!+响应);
    }
    功能OnErrorCall(响应){
        警报(response.status ++ response.statusText);
    }
    函数的HelloWorld(){
        VAR PAGEURL ='<%= RESOLVEURL(〜/ WebService的/ WebService.asmx)%>'        $阿贾克斯({
            网址:PAGEURL +/ HelloWorld的,
            成功:OnSuccessCall,
            错误:OnErrorCall
        });
    } < / SCRIPT>

当我运行这个code,我得到错误500.我缺少什么?

// ------------------------------- UPDATE ------------- ---------------------------------

啊!一个新的线索!如果移动的WebMethod到code-背后的Default.aspx,而不是作为一个WebService类的,但只是以[WebMethod]属性的方法的一部分,它的工作原理。 (至少我得到我的onSuccess功能OK)。但是,如果我再加入

 的contentType:应用/ JSON的;字符集= UTF-8,
数据类型:JSON

到$阿贾克斯()调用,我回让我的错误500(虽然它可能是一个不同的原因)。有一些安装说我失踪了JSON或什么作品呢?

// ------------------------------- UPDATE ------------- ---------------------------------
下面是实际的WebService。正如我前面所说,它的当你创建一个新项目,并选择Web Service你会得到什么。

如果有人能告诉我,这为他们工作,然后我可以专注于环境问题。

 使用系统;
使用System.Collections.Generic;
使用System.Linq的;
使用的System.Web;
使用System.Web.Services;///<总结>
///对WebService的摘要说明
///< /总结>
[WebService的空间(namespace =htt​​p://tempuri.org/)]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)
//要允许此Web服务从脚本调用,使用ASP.NET AJAX,取消注释以下行。
 [System.Web.Script.Services.ScriptService]
公共类的WebService:System.Web.Services.WebService {    公众的WebService(){        //取消注释以下行,如果使用设计的组件
        //的InitializeComponent();
    }    [的WebMethod]
    公共字符串的HelloWorld(){
        返回的Hello World;
    }}


解决方案

您要么没有尝试编译您的code或剪掉这里把你的实际code

  [的WebMethod]
公共字符串的HelloWorld(字符串名称){//< ---你忘记方法参数名来指定
    返回的Hello World;
}

因此​​,要么删除参数(即公共字符串的HelloWorld())或加名我在你的code表现。

另外,你没有任何参数,你可以把你的网址服务方法的浏览器,看看会发生什么。即转到 HTTP://localhost/yourapp/WebService/WebService.asmx/HelloWorld ,或任何真正的网址是

I'm struggling with my first WebService, so I decided to strip everything down to the basics. Still not working.

I created a brand new ASP.NET Website project.

I added a folder named WebService. In the WebService folder, I added a new WebService using the ASP.NET template. I modified the template only to uncomment the [System.Web.Script.Services.ScriptService].

In the Default.aspx page, after the "Welcome to ASP.NET", I added a panel as follows:

<asp:Panel ID="Panel1" runat="server" Height="200px" Width="200px" BackColor="Blue" onclick="HelloWorld();">
</asp:Panel>

I also added the following scripts to the HeaderContent area:

<script type="text/javascript" language="Javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>

<script type="text/javascript">

    function OnSuccessCall(response) {
        alert("Success! : " + response);
    }
    function OnErrorCall(response) {
        alert(response.status + " " + response.statusText);
    }
    function HelloWorld() {
        var pageUrl = '<%=ResolveUrl("~/WebService/WebService.asmx")%>'

        $.ajax({
            url: pageUrl + "/HelloWorld",
            success: OnSuccessCall,
            error: OnErrorCall
        });
    }

 </script>

When I run this code, I get Error 500. What am I missing?

//------------------------------- UPDATE ----------------------------------------------

Ahh! A new clue! If move the WebMethod into the code-behind for Default.aspx, not as part of a WebService class but just as a method with a [WebMethod] attribute, it works. (At least I get to my OnSuccess function OK). However, if I then add

contentType: "application/json; charset=utf-8",
dataType: "json",

to the $.ajax() call, I'm back to getting my Error 500 (although it might be for a different reason). Is there some install piece that I'm missing for JSON or something?

//------------------------------- UPDATE ---------------------------------------------- Here's the actual WebService. As I stated before, its exactly what you get when you create a New Item and select Web Service.

If someone can just tell me that this works for them, then I can focus on environment issues.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;

/// <summary>
/// Summary description for WebService
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// 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 WebService : System.Web.Services.WebService {

    public WebService () {

        //Uncomment the following line if using designed components 
        //InitializeComponent(); 
    }

    [WebMethod]
    public string HelloWorld( ) {
        return "Hello World";
    }

}

解决方案

You either didnt try to compile your code or didnt put your actual code here

[WebMethod]
public string HelloWorld(string name) { //<--- you forget method param name to specify
    return "Hello World";
}

So either remove parameters ( ie public string HelloWorld()) or add name as I showed in your code.

Also as you do not have any parameters you can just put url of your service method in the browser and see what happens. ie go to http://localhost/yourapp/WebService/WebService.asmx/HelloWorld, or whatever real URL is

这篇关于从调用$ JQuery的阿贾克斯一个WebService得到错误500的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆