阿贾克斯jQuery的asp.net错误的意外标记< [英] ajax jQuery asp.net error Unexpected token <

查看:129
本文介绍了阿贾克斯jQuery的asp.net错误的意外标记<的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法将Ajax功能添加到现有的asp.net 4的网站。 我都一直在aspx页面创建的WebMethod,也尝试过ASMX,但在这两种情况下,我得到这个错误意外标记<

这是我的jQuery的:

功能postAssets(datapm){

  $。阿贾克斯({
            键入:POST,
            超时:20000,
            tryCount:0,
            retryLimit:10,
            网址:talk.asmx / HelloWorld的,
            数据: {},
            的contentType:应用/ JSON的;字符集= UTF-8,
            数据类型:JSON,
            成功:函数(MSG){
                的console.log('成功postAssets'+ msg.d);
            },
            完成:功能(jqXHR,状态){
                如果(状态=='成功'||地位=='notmodified'){

                    的console.log('完整postAssets'+ jqXHR.responseText);
                }
            },
            错误:函数(REQ,状态,错误){

                的console.log(错误postAssets');
            }
        });
    }
 

这就是在ASMX:

 使用系统;
使用System.Collections.Generic;
使用System.Linq的;
使用的System.Web;
使用System.Web.Services;

///<总结>
///摘要说明谈话
///< /总结>
[WebService的(命名空间=htt​​p://tempuri.org/)]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)
//要允许此Web服务从脚本调用,使用ASP.NET AJAX,取消注释以下行。
// [System.Web.Script.Services.ScriptService]
公共类谈话:System.Web.Services.WebService {

    公开讲​​座(){

        //取消注释以下行,如果使用设计的组件
        //的InitializeComponent();
    }

    [WebMethod的]
    公共字符串的HelloWorld(){
        返回的Hello World;
    }

}
 

我不知道是否我缺少任何webconfig项目,或者是这一切在asp.net 4内置的?

 <结构>
  <的ConnectionStrings />
  <的System.Web>
    <编译调试=真正的targetFramework =4.0/>
    < machineKey中的validationKey =BA5B68AB87AAEA30753960733E796568decryptionKey =FAF15E4015737A7695D9761验证=SHA1/>
    <身份验证模式=窗口/>
  < /system.web>
  < system.webServer>
    <模块runAllManagedModulesForAllRequests =真/>
  < /system.webServer>
< /结构>
 

解决方案

您返回JSON或标记?你调用jQuery的阿贾克斯()方法需要JSON但是如果你返回的标记,与启动< 性格那么我能想象它抛出的异常。

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>

解决方案

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.

这篇关于阿贾克斯jQuery的asp.net错误的意外标记&LT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆