$ .ajax jquery没有显示函数中可能出错的成功函数数据

时间:2012-09-24 07:46:04

标签: jquery handler

我正在使用如下所示的jquery吗?处理程序正确地排除了代码并返回是一个json的形式(例如:[{“res”:1}])所以我想检查我是否检索任何成功的功能,所以最初只是发出警报,但警报本身没有显示,所以可能是错的?请你帮忙提前感谢你....

 <script type="text/javascript">

function DoneClick() {
    alert("OO");
    var checkLocation = "";
    var txtMM = document.getElementById("hdn_Tagging").value; ///Id s of textbox assigned in code behind MB--
    txtMM = txtMM.slice(0, - 1);
    var arrTxtMM = txtMM.split(",");

    for (var j = 0; j < arrTxtMM.length; j++) {
        var Loc = document.getElementById(arrTxtMM[j]).value;
        if (Loc == "") {
            checkLocation = "";
            break;
        } else {
           checkLocation += Loc + ":";
        }
    }
    if (checkLocation != "")
    {
        var url = 'Handler/newExifDetails.ashx?Id=' + txtMM + '&Location=' + checkLocation + '';
        alert(url);
        alert("yes");
         $(document).ready(function() {
            var url = 'http://localhost:4880/Handler/newExifDetails.ashx?Id=' + txtMM + '&Location=' + checkLocation + '';
                                $.ajax({
                                url : url,
                                type: "POST",
                                dataType: "json",
                                success: function(data) {
                                alert(data);
                                alert("yeah");
                                },
                                error: function(data) { alert("error"); }
                               });
                                }); 
          });

    }
     else
     {
        alert("Please pick the locations for all objects");
    }
}
                    </script>

这里的问题是调用Handler页面就好像我把断点放在Handler页面中并且正确执行....而response.write那里我得到的json字符串为:for ex:[{ “res”:1}]现在成功了:在$ .ajax中,如果我写了一个警告,它没有显示可能出错的地方???

Handler代码如下:

using System;
using System.Collections;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
using System.Web.Script.Serialization;
using System.Collections.ObjectModel;
using ClassLib_BLL;
using System.IO;
using System.Web.UI;
using System.Net;
using System.Collections.Generic;

namespace QlixooWeb.handler
{
    /// <summary>
    /// Summary description for $codebehindclassname$
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

    public class UserUploadsDTO
    {
        private int _res;
        public int res { get { return _res; } set { _res = value; } }
    }

    public class newExifDetails : IHttpHandler
    {
        public static readonly UserUploadsBLL oUserUploadsBll = new UserUploadsBLL();
        string[] splitQueryStr, arId, arLocation;
        public string Id1, Location1;

        public void ProcessRequest(HttpContext context)
        {
            //context.Response.ContentType = "text/plain";
            //context.Response.Write("Hello World");
            Collection<UserUploadsDTO> collection = new Collection<UserUploadsDTO>();
            UserUploadsDTO dto;
            string RId = context.Request.QueryString["Id"].ToString();
            string RLocation = context.Request.QueryString["Location"].ToString();
            //string json = new StreamReader(context.Request.InputStream).ReadToEnd();//Location = Bangalore%2C+Karnataka%2C+India%3AChittur%2C+Kerala%2C+India %3A  &  Id=4%2C94
            //splitQueryStr = json.Split('&');
            //Location1 = splitQueryStr[0];
            //Id1 = splitQueryStr[1];
            //string Location = RLocation.Substring(Location1.IndexOf('=', 0) + 1);
            string Location = RLocation;
            //Location = Location.Replace("%2C+", ",");
            //Location = Location.Replace("%3A", ":");
            //Location = Location.Replace("+", " ");
            Location = Location.TrimEnd(':');
            arLocation = Location.Split(':');
            string Id = RId;
            //string Id = RId.Substring(Id1.IndexOf('=', 0) + 1);
            //Id = Id.Replace("%2C", ",");
            arId = Id.Split(',');
            int res = 0;
            for (int i = 0; i < arId.Length; i++)
            {
                int UpId = Convert.ToInt32(arId[i]);
                Coordinate coordinate = Geocode.GetCoordinates(arLocation[i]);
                decimal latitude = coordinate.Latitude;
                decimal longitude = coordinate.Longitude;
                res = oUserUploadsBll.TaggingImages(latitude, longitude, UpId);
            }
            if (res > 0)
            {
                dto = new UserUploadsDTO();
                dto.res = res;
                collection.Add(dto);
            }
                //context.Response.Redirect("~/UserUploads.aspx");
                //context.Response.Write(res);
            else
                context.Response.Write("false");
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            string jsonString = serializer.Serialize(collection);
            context.Response.Write(jsonString);

        }
        public interface ISpatialCoordinate
        {
            decimal Latitude { get; set; }
            decimal Longitude { get; set; }
        }
        /// <summary>
        /// Coordiate structure. Holds Latitude and Longitude.
        /// </summary>
        public struct Coordinate : ISpatialCoordinate
        {
            private decimal _latitude;
            private decimal _longitude;
            public Coordinate(decimal latitude, decimal longitude)
            {
                _latitude = latitude;
                _longitude = longitude;
            }
            #region ISpatialCoordinate Members
            public decimal Latitude
            {
                get { return _latitude; }
                set { this._latitude = value; }
            }
            public decimal Longitude
            {
                get { return _longitude; }
                set { this._longitude = value; }
            }
            #endregion
        }
        public class Geocode
        {
            private const string _googleUri = "http://maps.google.com/maps/geo?q=";
            private const string _googleKey = "AIzaSyB4UgLW37a1jhxnnz5J27KPNaHIDmapSYk";
            private const string _outputType = "csv"; // Available options: csv, xml, kml, json
            private static Uri GetGeocodeUri(string address)
            {
                address = HttpUtility.UrlEncode(address);
                return new Uri(String.Format("{0}{1}&output={2}&key={3}", _googleUri, address, _outputType, _googleKey));
            }
            public static Coordinate GetCoordinates(string address)
            {
                WebClient client = new WebClient();
                Uri uri = GetGeocodeUri(address);
                /* The first number is the status code, 
                * the second is the accuracy, 
                * the third is the latitude, 
                * the fourth one is the longitude.
                */
                string[] geocodeInfo = client.DownloadString(uri).Split(',');
                return new Coordinate(Convert.ToDecimal(geocodeInfo[2]), Convert.ToDecimal(geocodeInfo[3]));
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}

同样在aspx页面中,我使用了2个脚本标记:

<script src="https://maps.googleapis.com/maps/api/js?sensor=false&libraries=places"></script>

<script src="js/jquery-latest.js" type="text/javascript"></script>

这是在aspx页面的head标签中使用的......我从哪里调用Handler ..

4 个答案:

答案 0 :(得分:0)

您还可以尝试以下方法:

$.post("http://localhost:4880/Handler/newExifDetails.ashx", { "Id": txtMm },
   function(data){
      alert('test');
   }
   ,"json"
);

http://api.jquery.com/jQuery.post/

这是写你现在写的东西的另一种方式。如果这不起作用,您可以尝试使用FireBug并查看FireBug所说的内容。

答案 1 :(得分:0)

尝试更改type:'GET'

因为你把它设置为POST,它会期望数据中的参数而不是查询字符串

var url = 'http://localhost:4880/Handler/newExifDetails.ashx;

     $.ajax({
         url: url,
         type: "POST",
         data : {'Id' : txtMM , 'Location' : checkLocation},

或者

var url = 'http://localhost:4880/Handler/newExifDetails.ashx?Id=' + txtMM + '&Location=' + checkLocation + '';

 $.ajax({
     url: url,
     type: "GET",

答案 2 :(得分:0)

在您发布的代码中没有错。我在小提琴中尝试了它并且它正常工作,请在此处查看:http://jsfiddle.net/zk8Nk/

您可能在页面中有一些其他脚本会阻碍,但如果没有看到整个页面,我无法帮助您。

答案 3 :(得分:0)

问题可能是jQuery ajax调用无法处理字符串中的“%2c”。