Ajax 调用 - 简单的 Web 服务方法返回整个页面



我的Web服务中有一个简单的函数。

[Web Method]   
public string TryWB()    
{       
    string sMsg = "try";
    return sMsg;
}

我从我的 aspx 页面调用它。 在顶部我试过

[ScriptMethod( ResponseFormat= ResponseFormat.Json)]

(或.xml甚至httpget(

在函数中,我也尝试用

sMsg = (new JavaScriptSerializer()).Serialize(sMsg);

甚至制作自定义对象只是为了包装消息,甚至尝试

JsonConvert.SerializeObject(cm,Newtonsoft.Json.Formatting.Indented);

从我的aspx页面中,文本,xml,json的所有组合都制成

$.ajax({       
    URL: 'FLMSWebService/Login.asmx/TryWB',
    method:"POST", (get also tried)
    contentType: "application/json; charset=utf-8",                    
    dataType: "text", 
    success: function (data) {                      
        alert('success');                      
        alert(JSON.stringify(data)); 
    },                    
    error: function (jqXHR, exception) {
        alert(jqXHR.responseText);
    }
});
contentType: json, xml tried
datatype : xml , json tried...

在 web.config 中也

<system.webServer>
    <modules>      
        <add name="ScriptModule" type="System.Web.Handlers.ScriptModule,
            System.Web.Extensions, Version=3.5.0.0, Culture=neutral,
            PublicKeyToken=31BF3856AD364E35"/>     
    </modules>

<system.web.extensions>
    <scripting>
        <webServices>
            <authenticationService enabled="true" />
        </webServices>
    </scripting>
</system.web.extensions>

这就是我在网上搜索 2 天得到的全部......但重点是,如果我有

dataType: "text",

这是一个成功,整页作为数据返回。 在所有其他数据类型或内容类型中,我收到错误,并且整页显示在

alert(jqXHR.responseText);

在错误回调中。

我被困住了!! 请帮忙

尝试一些更改。

method更改为type

如果您的服务位于名为 FLMSWebService 的文件夹中,请在前面添加一个斜杠,以便它从根目录中查找该文件夹。

将 dataType 更改为 json。默认情况下,asmx 返回 json。

删除JSON.stringify,添加data.d

$.ajax({
    type:"POST",
    url: '/FLMSWebService/Login.asmx/TryWB',        
    contentType: "application/json; charset=utf-8",                    
    dataType: "json", 
    success: function (data) {                      
        alert('success');                      
        alert(data.d); 
    },                    
    error: function (jqXHR, exception) {
        alert(jqXHR.responseText);
    }
});

嗨,我已经为您的问题创建了类似的副本,这是工作。

调试问题的步骤。

  1. 检查浏览器控制台是否有任何错误。
  2. 检查您的服务 URL 是否正确,否则提供完整的 URL 并检查,示例

网址:"http://localhost:50081/FLMSWebService/Login.asmx/HelloWorld">

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
    function ShowResponse() {
        $.ajax({
            type: "POST",
            url: "/FLMSWebService/Login.asmx/HelloWorld",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: OnSuccess,
            failure: function (response) {
                alert(response.d);
            }
        });
    }
    function OnSuccess(response) {
        alert(response.d);
    }
</script>
<input id="btnGetTime" type="button" value="Show Current Time"
    onclick="ShowResponse()" />

登录.asmx 服务

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
namespace WebApplication1.FLMSWebService
{
    /// <summary>
    /// Summary description for Login
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // 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 Login : System.Web.Services.WebService
    {
        [WebMethod]
        public string HelloWorld()
        {
            return "Hello World";
        }
    }
}

最新更新