简体   繁体   中英

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

I am net to asp.net and web-services. I am developing HTML 5 application which calls asp.net web-service. 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.

Following is my web service code:

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:

// 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:

<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.

firstly, there is no such attribute as Content-Type . Use contentType .

secondly, you have specified wrong structure of your url. Structure of the ajax url should be like this:

   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,

   ~/ServiceName.asmx/MethodName?anyParamters=value

change your ajax function to this:

$.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 . Give it a go

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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