vb.net 调用 webservice 时,服务器响应状态为 500(内部服务器错误)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17597571/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 14:13:51  来源:igfitidea点击:

the server responded with a status of 500 (Internal Server Error) when calling webservice

asp.netvb.netweb-servicesdojo

提问by vbNewbie

I do not have much experience with web applications and just when I thought I have it working something is going wrong. I studied a demo online in C# and then created my own simple version which seems to work fine. All it does is take some input and pass it to the webservice. I then tried implementing a similar webservice in a more complex application and I get the following error with no clear indication as to why it does not work:

我对 Web 应用程序没有太多经验,就在我认为它可以正常工作时,出现了问题。我在 C# 中在线研究了一个演示,然后创建了我自己的简单版本,它似乎工作正常。它所做的只是获取一些输入并将其传递给网络服务。然后我尝试在更复杂的应用程序中实现类似的 web 服务,但我收到以下错误,但没有明确说明为什么它不起作用:

the server responded with a status of 500 (Internal Server Error) 

This is within the browser debugger and it does not clarify why.

这是在浏览器调试器中,它没有说明原因。

webservice url :hostlocal:xxxxx/SVC/contact.asmx/ContactMessage

网络服务网址:hostlocal:xxxxx/SVC/contact.asmx/ContactMessage

my webservice code as follows:

我的网络服务代码如下:

<System.Web.Services.WebService(Namespace:="http://tempuri.org/")> _
<System.Web.Services.WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
 <ToolboxItem(False)> _
Public Class contact
Inherits System.Web.Services.WebService

<WebMethod()> _
Public Function ContactMessage(ByVal clientProj As String, email As String, message As String) As String


    Dim insert_succes As Integer = load_data("", clientProj, "", "", "", "", "", "", "", "", email, message, "")
    Dim passedValidation As Boolean = True

    ' here you can return a flag which you can handle on client side accordingly
    ' if you need to

    Return If(passedValidation, "1", "0")

End Function

and the javascript that calls it:

以及调用它的 javascript:

var dojoXhr;

 require(["dojo/parser", "dojo/query", "dojo/dom-class", "dojo/dom-style", 
    "dojo/on", "dojo/_base/event",
"dojo/request/xhr", "dijit/form/ValidationTextBox", "dojo/domReady!"],
function (parser, query, domClass, domStyle, on, event, xhr) {

    parser.parse();

    var btnSubmit = document.getElementById('btnSubmit');

    function correctInput(div, td, msg) {
        domStyle.set(div, 'display', '');
        td.innerHTML = msg;
    }

    on(btnSubmit, 'click', function (e) {
        event.stop(e);
        var clientProj = dijit.byId("clientName").get("value");
        var clientKey = dijit.byId("clientKey").get("value");
        var accessToken = dijit.byId("accessToken").get("value");
        var lusername = dijit.byId("lusername").get("value");
        var lpassword = dijit.byId("lpassword").get("value");
        var provid = dijit.byId("provID").get("value");




        var feedback = document.getElementById('feedback');
        var feedbackTD = query('td.feedback')[0];

        domStyle.set(feedback, 'display', 'none');

        if (!validateEmail(lusername)) {
            correctInput(feedback, feedbackTD, 'Please enter a valid email.');
            return;
        }

        var port = document.location.port;
        var xhrPath = '//' + document.location.hostname + (port == (80 || 443) ? '/' : ':' + port + '/') + 'SVC/contact.asmx/ContactMessage';

        var msgbody = {
            clientProj: clientProj,
            clientKey: clientKey,
            accessToken: accessToken,
            lusername: lusername,
            lpassword: lpassword,
            provid: provid
        };

        xhr(xhrPath, {
            headers: { "Content-Type": "application/json; charset=utf-8" },
            method: 'post',
            data: JSON.stringify(msgbody)
        }).then(function (data) {
            if (data == "0") {
                correctInput(feedback, feedbackTD, 'Your message could not be sent.');
                return;
            }
            alert('Bcrap STILL NOT WORKING NOW!');
            // show feedback to the user
            domStyle.set(feedback, 'display', '');
            domStyle.set(document.getElementById('msgBodyOutter'), 'display', 'none');
            feedbackTD.innerHTML = "Message was sent successfully.";
        });
    })
});

采纳答案by vbNewbie

I figured out what the problem was and it was related to the xhr function. Basically the parameters passed has to match in name and number in the code behind.

我想出了问题所在,它与 xhr 函数有关。基本上,传递的参数必须在后面的代码中匹配名称和编号。

回答by Cortright

Your service could do with some exception handling to trap an error and write it to the event log (or something) so you can see what's going on. Alternatively, run it in Debug Mode through Visual Studio and issue your test -- it should catch at the breakpoint and allow you to step through to see what the problem is.

您的服务可以通过一些异常处理来捕获错误并将其写入事件日志(或其他内容),以便您可以查看发生了什么。或者,通过 Visual Studio 在调试模式下运行它并发出您的测试——它应该在断点处捕获并允许您逐步查看问题所在。