Registered User
Registered User

Reputation: 3699

ajax jQuery asp.net error Unexpected token <

I am having trouble adding ajax functionality to existing asp.net 4 website. I have both tried creating webmethod in the aspx page and also tried asmx, but in both cases I get this error Unexpected token <

this is my jQuery:

function 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');
            }
        });
    }

and this is what is in 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";
    }

}

I wonder if I am missing any webconfig items, or is it all built in in 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>

Upvotes: 2

Views: 1403

Answers (2)

dr.Crow
dr.Crow

Reputation: 1563

I guess the problem is that you declare your Ajax type as POST while in your ASP Controller you declare HelloWorld() as WebMethods. That's why your ajax can't find your HelloWorld Function.

Try to delete this line:

[WebMethods]

and see if that works.

Upvotes: 0

user99874
user99874

Reputation:

Are you returning JSON or markup? Your call to jQuery's ajax() method is expecting JSON but if you're returning markup that starts with a < character then I could imagine it throwing that exception.

Upvotes: 2

Related Questions