0

我正在使用如下所示的 jquery,它将转到 ?Handler 正确执行代码并返回格式为(例如:[{"res":1}])的 json,所以我想检查我是否成功检索到任何内容功能所以最初只是发出警报,但警报本身没有显示,所以可能出了什么问题?你能帮忙提前谢谢你吗....

 <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>

这里的问题是,处理程序页面被调用,就好像我将断点放在处理程序页面中它正确地运行和执行......并且 response.write 在那里我得到了 json 字符串:例如:[{“res” :1}] 现在成功了:在 $.ajax 中,如果我写了一个警报,它没有显示出什么可能是错的???

处理程序代码如下:

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 标记中使用......我从这里调用处理程序..

4

4 回答 4

0

您还可以尝试以下方法:

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

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

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

于 2012-09-24T07:56:10.403 回答
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",
于 2012-09-24T07:56:52.043 回答
0

在您发布的代码中没有任何问题。我用小提琴试了一下,它工作正常,在这里看到它:http: //jsfiddle.net/zk8Nk/

您可能在页面中有一些其他脚本妨碍您,但如果不查看整个页面,我将无法帮助您。

于 2012-09-24T07:58:50.363 回答
0

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

于 2013-10-08T12:27:30.427 回答