asp.net - ajax jQuery asp.net 错误意外的标记 <

标签 asp.net ajax web-services jquery

我在向现有的 asp.net 4 网站添加 ajax 功能时遇到问题。 我都尝试过在 aspx 页面中创建 webmethod 并尝试过 asmx,但在这两种情况下我都会收到此错误 Unexpected token <

这是我的 jQuery:

函数 postAssets(datapm) {

        $.ajax({
            type: "POST",
            timeout: 20000,
            tryCount: 0,
            retryLimit: 10,
            url: "talk.asmx/HelloWorld",
            data: "{}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (msg) {
                console.log('success postAssets '+msg.d);
            },
            complete: function (jqXHR, status) {
                if (status == 'success' || status == 'notmodified') {

                    console.log('complete postAssets' + jqXHR.responseText);
                }
            },
            error: function (req, status, error) {

                console.log('error postAssets');
            }
        });
    }

这就是 asmx 中的内容:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;

/// <summary>
/// Summary description for talk
/// </summary>
[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 talk : System.Web.Services.WebService {

    public talk () {

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

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

}

我想知道我是否缺少任何 webconfig 项,或者它全部内置于 asp.net 4 中吗?

<configuration>
  <connectionStrings />
  <system.web>
    <compilation debug="true" targetFramework="4.0" />
    <machineKey validationKey="BA5B68AB87AAEA30753960733E796568" decryptionKey="FAF15E4015737A7695D9761" validation="SHA1" />
    <authentication mode="Windows" />
  </system.web>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true" />
  </system.webServer>
</configuration>

最佳答案

您返回的是 JSON 还是标记?您调用 jQuery 的 ajax()方法需要 JSON,但如果您返回以 < 开头的标记那么我可以想象它会抛出该异常。

关于asp.net - ajax jQuery asp.net 错误意外的标记 <,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11075862/

相关文章:

ajax - 如何使 Flask/保持 Ajax HTTP 连接处于事件状态?

javascript - 在大量使用 javascript 的应用程序中实现 SEO 友好性策略的一个很好的例子是什么?

php - CodeIgniter网站+phonegap实践

asp.net web 应用程序启动时间。如何优化?

javascript - 如何使用 ASP.NET Web 服务仅刷新 ASP.NET 4.0 网页的一小部分而不编辑任何其他部分

javascript - Ajax 完全没有被调用

c++ - 使用 C++ 的 Web 服务

asp.net - Telerik RadWindow Javascript 将值返回到 ASP.NET

asp.net - 如何防止 DIV 中文字中的 HTML 代码影响外部页面样式

java - 使用 Ant 从 WSDL 生成客户端 jar 时出现问题