运行简单的Web服务时出错 [英] Getting a error when running a simple web service

查看:83
本文介绍了运行简单的Web服务时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在C#中构建了一个非常简单的Web服务,这是服务器代码



I have constructed a very simple web service in C# this is the server code

using MySql.Data;
[WebService(Namespace = "http://www.example.org/", Description = "Example web service in C#")]
	[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
	[ScriptService]
	public class ExampleService : System.Web.Services.WebService
	{
        
    	[WebMethod(Description = "Echos back a string")]
		[ScriptMethod(UseHttpGet = false, ResponseFormat = ResponseFormat.Json)]

		public string echoString(String testString)
		{
			// An example of a pragma for throwing an exception for invalid input
			// NB: You will still get basic type error handling even if you don't do this!
			// "InputValidationException" is a commonly used pragma for input handling in .NET
			if (testString.Length < 1)
				throw new InputValidationException("The input string must be at least one character");

			return testString; 
		}
       }





当我通过Web浏览器访问ExampleServics.asmx时,它工作正常。但是,当我尝试使用下面的代码通过javascript访问它时,我得到以下错误

根级别的数据无效。第1行,第1位。据我所知,这是由于语句错误造成的。以下是请求的javascript



When I access ExampleServics.asmx via a Web browser it works fine. However when I try to access it via javascript using the code below i get the following error
Data at the root level is invalid. Line 1, position 1. It is my understanding that this is caused by a malformed statement. Here is the javascript for the Request

var str = 
   'POST /ExampleService.asmx HTTP/1.1 ' +
     'Host: localhost ' +
     'Content-Type: text/xml; charset=utf-8 ' +
     'Content-Length: length ' +
     'SOAPAction: "http://www.example.org/echoString" ' +
     '<?xml version="1.0" encoding="utf-8"?> ' +
     '<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
      xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
      xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> ' +
     '<soap:Body> ' +
     '<echoString xmlns="http://www.example.org/"> ' +
     '<testString>test</testString> ' +
     '</echoString> ' +
     '</soap:Body> ' +
     '</soap:Envelope> ';

        var url = "http://apislot/ExampleService.asmx";
        var soap_request =  makeSoapRequest(url);
        var xhr = new XMLHttpRequest();
        xhr.open("POST", url, true);
         xhr.onreadystatechange = function() {//Call a function when the state changes.
            // Request finished. Do processing here.
        }
       xhr.send(str);





我曾尝试过:



我一直试图解决这个问题,但似乎没什么用。一直在互联网上,但我找到的只是一个错误的请求,但我发现上面的代码没有错。请帮忙。



What I have tried:

I have been trying to solve this for days but nothing seems to work. Been all over the internet but all I find is references to a bad request but I can find nothing wrong with the code above. Please help.

推荐答案

正如评论中所提到的,我建议使用jQuery的ajax()调用。它为您处理复杂性。



很高兴听到您的工作正常。
As mentioned in the comments, I would suggest using jQuery's ajax() call. It handles the complexities for you.

Glad to hear you got it working.


这篇关于运行简单的Web服务时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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