2

既存のasp.net4Webサイトに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.net4に組み込まれているのでしょうか。

<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またはマークアップを返しますか?jQueryのajax()メソッドへの呼び出しはJSONを期待していますが、文字で始まるマークアップを返す場合は、<その例外をスローすることを想像できます。

于 2012-06-18T00:42:41.867 に答える
0

POST問題は、ASPコントローラーHelloWorld()でWebMethodsとして宣言するのと同じようにAjaxタイプを宣言することだと思います。そのため、ajaxはHelloWorld関数を見つけることができません。

この行を削除してみてください:

[WebMethods]

それが機能するかどうかを確認します。

于 2015-11-02T04:06:06.737 に答える