jQuery.ajax() parsererror

前端 未结 3 2048
时光说笑
时光说笑 2020-12-05 03:35

when i try to get JSON from http://api-v3.deezer.com/1.0/search/album/?q=beethoven&index=2&nb_items=2&output=json with:

(jQuery 1.6.2)

$.ajax({
         


        
3条回答
  •  独厮守ぢ
    2020-12-05 03:59

    At last i have found the solution. First of all, the webmethods in a webservice or page doesn't work for me, it always returns xml, in local works fine but in a service provider like godaddy it doesn't.

    My solution was to create an .ahsx, a handler in .net and wrap the content with the jquery callback function that pass the jsonp, and it works .

    [System.Web.Script.Services.ScriptService]
    public class HandlerExterno : IHttpHandler
    {
        string respuesta = string.Empty;
    
        public void ProcessRequest ( HttpContext context )
        {
    
    
           string  calls=  context.Request.QueryString["callback"].ToString();
    
             respuesta = ObtenerRespuesta();
            context.Response.ContentType = "application/json; charset=utf-8";
            context.Response.Write( calls +"("+    respuesta +")");
        }
    
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    
        [System.Web.Services.WebMethod]
        private string ObtenerRespuesta ()
        {
    
    
    
            System.Web.Script.Serialization.JavaScriptSerializer j = new System.Web.Script.Serialization.JavaScriptSerializer();
    
    
            Employee[] e = new Employee[2];
            e[0] = new Employee();
            e[0].Name = "Ajay Singh";
            e[0].Company = "Birlasoft Ltd.";
            e[0].Address = "LosAngeles California";
            e[0].Phone = "1204675";
            e[0].Country = "US";
            e[1] = new Employee();
            e[1].Name = "Ajay Singh";
            e[1].Company = "Birlasoft Ltd.";
            e[1].Address = "D-195 Sector Noida";
            e[1].Phone = "1204675";
            e[1].Country = "India";
    
            respuesta = j.Serialize(e).ToString();
            return respuesta;
    
        }
    
    }//class
    
    public class Employee
    {
        public string Name
        {
            get;
            set;
        }
        public string Company
        {
            get;
            set;
        }
        public string Address
        {
            get;
            set;
        }
        public string Phone
        {
            get;
            set;
        }
        public string Country
        {
            get;
            set;
        }
    }
    

    And here is the call with jquery:

    $(document).ready(function () {
        $.ajax({
            // url: "http://www.wookmark.com/api/json",
            url: 'http://www.gjgsoftware.com/handlerexterno.ashx',
            dataType: "jsonp",
    
    
            success: function (data) {
                alert(data[0].Name);
            },
            error: function (data, status, errorThrown) {
                $('p').html(status + ">>  " + errorThrown);
            }
        });
    });
    

    and works perfectly

    Gabriel

提交回复
热议问题