2

我无法将 ajax 功能添加到现有的 asp.net 4 网站。我都尝试在 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>
4

2 回答 2

2

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

于 2012-06-18T00:42:41.867 回答
0

我想问题是你声明你的 Ajax 类型,POST而在你的 ASP 控制器中你声明HelloWorld()为 WebMethods。这就是为什么您的 ajax 找不到您的 HelloWorld 函数的原因。

尝试删除这一行:

[WebMethods]

看看这是否有效。

于 2015-11-02T04:06:06.737 回答