简体   繁体   English

来自外部HTML页面的asp.net Web服务调用未定义错误

[英]Undefined Error on asp.net web-service call from external HTML page

I am net to asp.net and web-services. 我对asp.net和网络服务有所了解。 I am developing HTML 5 application which calls asp.net web-service. 我正在开发调用asp.net Web服务的HTML 5应用程序。 I have published my asp.net web service on IIS7 and it works fine, but when I am calling web service through external HTML 5 JQuery it gives me undefined error. 我已经在IIS7上发布了asp.net Web服务,并且运行良好,但是当我通过外部HTML 5 JQuery调用Web服务时,它给了我未定义的错误。

Following is my web service code: 以下是我的Web服务代码:

using System;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;

[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 Service : System.Web.Services.WebService
{
    public Service () {

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

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


}

My Jquery Code Is: 我的Jquery代码是:

// JavaScript Document
 $(document).ready(function() {

         $.ajax({
                type: "POST",
                url: "http://localhost/mywebserice/Service.asmx?op=HelloWorld",
                Content-Type: 'application/x-www-form-urlencoded',
                dataType: "xml",
                data: '{}',
                 success: function(){
                    alert("Success");
                },
                error: function(){
                    alert("Error");
                }
        });
});

My HTML5 Code IS: 我的HTML5代码是:

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script src="jquery-1.10.2.js"></script>
<script type="text/javascript" src="newJS.js"></script>
</head>

<body>
</body>

</html>

Can any one please help me to solve this issue? 谁能帮我解决这个问题?

Your jQuery ajax setting attributes are wrong. 您的jQuery ajax设置属性错误。

firstly, there is no such attribute as Content-Type . 首先,没有诸如Content-Type这样的属性。 Use contentType . 使用contentType

secondly, you have specified wrong structure of your url. 其次,您指定的网址结构错误。 Structure of the ajax url should be like this: Ajax网址的结构应如下所示:

   domain/ServiceName.asmx/MethodName?anyParamters=value

you can also specify relative url, if the page from which you are calling the webservice and the webservice belong to the same domain i,e, 如果您要从中调用该Web服务的页面与该Web服务属于同一域(例如,

   ~/ServiceName.asmx/MethodName?anyParamters=value

change your ajax function to this: 将您的ajax函数更改为此:

$.ajax({
         type: "POST",
         url: "http://localhost/mywebserice/Service.asmx/HelloWorld",
         contentType: 'application/x-www-form-urlencoded',
         dataType: "xml",
         data: {},
         success: function (msg) {
              alert($(msg).text());
              //console.log($(msg).text());
         },
         error: function(xhr, status, error){
                  console.log("Error");
          }
});

you can read about all the possible attributes of jQuery ajax here . 您可以在此处了解jQuery ajax的所有可能属性。 Give it a go 搏一搏

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

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