首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >从外部asp.net页面调用HTML服务时出现未定义的错误

从外部asp.net页面调用HTML服务时出现未定义的错误
EN

Stack Overflow用户
提问于 2013-12-16 18:36:08
回答 1查看 1.2K关注 0票数 0

我是asp.net和网络服务的网络用户。我正在开发调用asp.net web服务的HTML5应用程序。我已经在IIS7上发布了我的asp.net web服务,它工作得很好,但是当我通过外部HTML5 JQuery调用web服务时,它给了我未定义的错误。

以下是我的web服务代码:

代码语言:javascript
运行
复制
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";
    }


}

我的Jquery代码是:

代码语言:javascript
运行
复制
// 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");
                }
        });
});

我的HTML5代码是:

代码语言:javascript
运行
复制
<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>

有谁能帮我解决这个问题吗?

EN

Stack Overflow用户

发布于 2013-12-16 18:48:52

您的jQuery ajax设置属性错误。

首先,没有Content-Type这样的属性。使用contentType

其次,您指定了错误的url结构。ajax url的结构应该如下所示:

代码语言:javascript
运行
复制
   domain/ServiceName.asmx/MethodName?anyParamters=value

如果调用webservice的页面和webservice属于同一个域,也可以指定相对url。

代码语言:javascript
运行
复制
   ~/ServiceName.asmx/MethodName?anyParamters=value

将ajax函数更改为:

代码语言:javascript
运行
复制
$.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");
          }
});

您可以阅读有关jQuery ajax here的所有可能属性。试一试

票数 0
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20608713

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档