将字符串传递并返回到VB.NET Web方法 [英] Pass And Return A String Into VB.NET Web Method

查看:65
本文介绍了将字符串传递并返回到VB.NET Web方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用AJAX从Javascript发送简单字符串并将其返回到VB.NET Web方法.这是我正在使用的Javascript/jQuery脚本:

I am trying to send and return a simple string to a VB.NET web method from Javascript using AJAX. Here is the Javascript/jQuery script I am using:

function jQuerySerial() {
//I SET A VARIABLE TO THE STRING I WANT TO PASS INTO MY WEB METHOD
var str = "Hello World";

//AND TRY TO PASS IT INTO MY VB.NET WEB METHOD
$.ajax({
    type: "POST",
    url: "test_WebService.asmx/testWebService",
    data: str,
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function (e) {
        alert("It worked: " + e);
    },
    error: function (e) {
        alert("There was an error retrieving records: " + e);
    }
});
}//END jQuerySerial

这是非常简单的VB.net Web方法.Web方法除了获取字符串然后将其传递回Javascript之外什么都不做:

And here is the very simple VB.net Web Method. The Web Method does nothing but get the string and then pass it back to Javascript:

   <WebMethod( )> _
Public Function testWebService(str As String) As String
    Return str
End Function

当我尝试运行此错误时:函数触发并返回一条消息,提示:

When I attempt to run this the error: function fires and returns a message saying:

"There was an error retrieving records: [object Object]"

在同一Web服务类中,我还有许多其他Web方法可操纵数据库记录,并且它们都可以工作.但是,这是我尝试使用$ .ajax语法编写的第一个脚本,并将某些内容返回给调用Javascript,因此我对这里的错误完全一无所知.

I have many, many other Web Methods in this same Web Service class that manipulate database records and they all work. But, this is the first one I have ever tried to write using the $.ajax syntax and return something to the calling Javascript so I am completely clueless on whats wrong here.

任何有关如何进行这项工作的建议将不胜感激.谢谢

Any suggestions on how to make this work would be appreciated. Thanks

推荐答案

看起来这里的问题是,当您期望Web服务期望一个JSON对象时,您正在向Web服务传递一个简单的字符串.请参阅有关jQuery和ASP.NET Web服务的常见问题的本文(特别是项目2):

It looks like the issue here is that you're passing a simple string to the Web Service when it is expecting a JSON object. See this article on common issues with jQuery and ASP.NET web services (specifically item 2):

http://encosia.com/3-在使用带有aspnet-ajax的jquery-时要避免使用错误/

这篇关于将字符串传递并返回到VB.NET Web方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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