2011-11-28 165 views
1

我有一个AJAX国家/城市选择。当我选择美国的浏览器崩溃。卫生署!太多的JSON崩溃的浏览器:(

我有个国家的下拉列表。当我选择一个国家一个jQuery AJAX调用运行它获取属于这个国家的城市的JSON响应。

我应该看到它的到来,当我不得不增加执行过程中我的允许的记忆。下面是选择英国JSON响应。

{"5947":"Aberdeen","12838":"Aberystwyth","15707":"Aldershot","18575":"Alsagers Bank","18682":"Altrincham","4863":"Andover","41802":"AOL","6471":"Armagh","18945":"Ascot","4864":"Ashby-de-la-Zouch","4865":"Ashford","5948":"Aviemore","12985":"Aylesbury","12281":"Ballymena","14446":"Banbury","12445":"Bangor","13631":"Barking","4866":"Barnet","17004":"Barnsley","16423":"Barrow-in-Furness","16254":"Basildon","12402":"Basingstoke","5826":"Bath","13289":"Beddgelert","15082":"Bedford","4868":"Belfast","4869":"Belper","13874":"Benfleet","5827":"Benson","15514":"Berkhamsted","4870":"Berwick Upon Tweed","12948":"Betws-y-Coed","18776":"Bexley","14530":"Bicester","4871":"Billericay","18436":"Birkenhead","4872":"Birmingham","14592":"Blackburn","14686":"Blackpool","12526":"Bolton","12480":"Bournemouth","13062":"Bracknell","18772":"Bradford","4873":"Braemar","4874":"Brecon","4875":"Brentwood","18820":"Brighton","14260":"Bristol","4876":"Broomfield","42004":"Burgess Hill","14654":"Burnley","4877":"Burton Upon Tren","13812":"Bury","15835":"Bury St Edmunds","16500":"Camberley","4878":"Cambridge","4879":"Canterbury","5957":"Cardiff","14443":"Carlisle","14065":"Carrickfergus","42384":"Chalgrove","5832":"Chatham","13641":"Chelmsford","4880":"Cheltenham","4881":"Chester","42879":"Chesterfield","12160":"Chichester","41768":"Chorley","14056":"Church Stretton","5949":"Cladich","4884":"Colchester","16204":"Congleton","17534":"Coniston","42888":"Corsham","4885":"Coventry","13575":"Crawley","15410":"Crewe","13913":"Croydon","4886":"Cumbernauld","13711":"Dartford","4887":"Dartmouth","5833":"Derby","17468":"Derry","4889":"Doncaster","13696":"Dorchester","15377":"Dorking","5834":"Dover","16659":"Dudley","41867":"Dumbarton","18091":"Dumfries","4890":"Dunbar","14217":"Dunblane","4891":"Dundee","14067":"Dunfermline","4892":"Durham","16058":"East Molesey","17521":"East Preston","12501":"Eastbourne","12374":"Eastrea","4893":"Edinburgh","18992":"Elgin","41763":"Ellesmere","12883":"Ely","16825":"Enfield","14510":"Epsom","5835":"Exeter","4894":"Falkirk","5836":"Falmouth","42388":"Faringdon","42034":"Farmington","14604":"Farnham","42347":"Feltham","12829":"Fleet","4895":"Forres","42315":"Frosterley","5950":"Glasgow","4896":"Glastonbury","12562":"Gloucester","15956":"Gosport","4898":"Grangemouth","12626":"Gravesend","16057":"Grays","4899":"Great Wilbraham","4900":"Greenock","12752":"Grimsby","11747":"Guildford","14506":"Guilford","11938":"Halifax","5010":"Hamilton","15553":"Harlow","41733":"Harpenden","14713":"Harrow","4902":"Hartlepool","18952":"Haslemere","13977":"Hastings","14917":"Hatfield","12529":"Haverfordwest","4903":"Haverhill","4904":"Hawarden","5951":"Hawick","11776":"Hemel Hempstead","15302":"Hereford","14999":"Hertford","41893":"Heston","16056":"Hexham","13019":"High Wycombe","13643":"Hoddesdon","5958":"Holyhead","12420":"Hornchurch","14160":"Horsham","12108":"Huddersfield","5837":"Hull End","13296":"Huntingdon","14801":"Hyde","17707":"Ilford","41721":"Inverkeithing","4905":"Inverness","5838":"Ipswich","4906":"Keighley","4907":"Kelso","18628":"Kendal","17805":"Kenilworth","4908":"Kennet","4909":"Kettering","18578":"Kidsgrove","18984":"Kilmarnock","4910":"Kingston Upon Hull","5952":"Kirkwall","18257":"Lakenheath","15425":"Lampeter","13182":"Lancaster","4911":"Laughton","13488":"Leamington","18824":"Leeds","13135":"Leek","17849":"Leicester","17716":"Leigh","12836":"Lerwick","13387":"Letchworth","4912":"Lewes","41767":"Leyland","13546":"Lichfield","5840":"Lincoln","19039":"Little Chalfont","16778":"Liverpool","13442":"Llandrindod Well","5953":"Loch Ness","12008":"London","15035":"Loughborough","15518":"Loughgall","15011":"Louth","18492":"Lowestoft","14023":"Luton","4913":"Machynlleth","12416":"Maidenhead","12230":"Maidstone","14722":"Manchester","4914":"Mansfield","4915":"Margate","4916":"Marlborough","17889":"Marlow","18870":"Melborne","16170":"Melton Mowbray","4917":"Merton","5844":"Middlesbrough","5959":"Milford","15181":"Millom","12315":"Milton Keynes","12089":"Mold","18816":"Montrose","5954":"Motherwell","18574":"Nantwich","4918":"Newark","17097":"Newbury","5845":"Newcastle","4919":"Newcastle Upon Tyne","19040":"Newport","41682":"Newquay","13629":"Northallerton","4922":"Northampton","18577":"Northwich","42209":"northwold","15080":"Norwich","5847":"Nottingham","4923":"Oban","11975":"Oldham","6474":"Omagh","17161":"Oxford","15422":"Oxshott","18627":"Penrith","4925":"Penzance","16404":"Perth","5848":"Peterborough","4926":"Plains","4927":"Plymouth","15551":"Pontypridd","14208":"Poole","4928":"Portsmouth","17642":"Portstewart","41766":"Preston","5011":"Prestwick","18579":"Radway Green","42069":"Ramsgate","11775":"Reading","14706":"Redditch","16276":"Ringwood","15522":"Ripon","14673":"Rochester","15968":"Romford","41857":"Rugby","15289":"Runcorn","17520":"Rustington","14052":"Saint Albans","16462":"Salford","4931":"Salisbury","42295":"Sandwich","17690":"Sandy","4932":"Scarborough","13975":"Seaford","12003":"Shaftesbury","18891":"Sheffield","5850":"Shrewsbury","13178":"Slough","14708":"Solihull","4935":"Southampton","4936":"Southborough","14524":"Southend-on-Sea","13970":"Southport","42260":"St Albans","5955":"St Andrews","15841":"St Asaph","18576":"St Helens","16114":"St Ives","12717":"Stafford","41746":"Staines","14051":"Stanmore","16656":"Stansted","42032":"Stevenage","5012":"Stirling","11801":"Stockport","14198":"Stockton-on-Tees","4937":"Stoke On Trent","42386":"Stranraer","4938":"Stratford-Upon-Avon","4939":"Stroud","18615":"Sudbury","11860":"Sunderland","16393":"Sutton","5960":"Swansea","12853":"Swindon","4941":"Taunton","5851":"Teeside","13973":"Telford","4943":"Truro","17702":"Virginia Water","5852":"Waddington","12059":"Wakefield","4945":"Wallingford","4947":"Wareham","5853":"Warrington","4948":"Warwick","4949":"Watford","12009":"Wellingborough","12528":"Wellington","13366":"Wells","12530":"Welwyn Garden City","16785":"Weston Under Lizard","16334":"Wetherby","18171":"Weymouth","4950":"Whitby","13308":"Whitehaven","42387":"Whitehead","5956":"Wick","17581":"Wilmslow","5854":"Wimbledon","12524":"Wimborne Minster","12551":"Winchester","15946":"Windsor","18573":"Winsford","4952":"Wisbech","4953":"Wisborough Green","12982":"Woking","18769":"Wokingham","13287":"Wolverhampton","17904":"Woodford","18086":"Woolavington","11783":"Worcester","12128":"Worthing","5961":"Wrexham","13630":"Yarm","17015":"Yeovil","11824":"York"} 

这是我的Javascipt:

$('#current-country').change(function(){ //any select change on the dropdown with id country trigger this code 
      $('.select-current-city').show(); 
      $("#current-cities > option").remove(); //first of all clear select items 
      var country_id = $('#current-country').val(); // here we are taking country id of the selected one. 
      $.ajax({ 
       type: "POST", 
       url: "<?php echo base_url()?>map/get_cities/"+country_id, //here we are calling our user controller and get_cities method with the country_id 

       success: function(cities) //we're calling the response json array 'cities' 
       { 
        $.each(cities,function(id,city) //here we're doing a foeach loop round each city with id as the key and city as the value 
        { 
         var opt = $('<option />'); // here we're creating a new select option with for each city 
         opt.val(id); 
         opt.text(city); 
         $('#current-cities').append(opt); //here we will append these new select options to a dropdown with the id 'cities' 
        }); 
       } 

      }); 

     }); 

有没有人对我如何在浏览器中处理这么多数据有什么建议?

我使用PHP(Codeigniter),MySQL和jQuery。

+0

多少数据? HTTP响应的大小是多少? (检查浏览器开发人员工具中的网络选项卡) –

+2

这只有6K。即使IE可以处理!你的问题在于别处 –

+0

那不是那么多的数据 - 你确定问题是检索响应而不处理它?如果这太大,无法接收您的浏览器不会崩溃 - 它会挂起。最后 - 你忘了提及你正在看到这个浏览器。 – rlemon

回答

2

我建议创建一个新的option节点的数组,然后将它们附加到集群。一次只做一个可能会让你失望。

var newOptions = []; 
$.each(cities,function(id,city) { 
    var opt = $('<option />', { "val": id, "text": city }); 
    newOptions.push(opt[0]); //need to push actual dom node - thanks RightSaidFred 
}); 

$('#current-cities').append(newOptions); 

或者应该在下拉菜单中清除以前的选项?如果是这样的话:

$('#current-cities').html(newOptions); 
+0

你不能像这样使用'.join()'。你最终会用'[object object] [object Object] [object Object] ...“' – RightSaidFred

+0

@RightSaidFred - 刚才意识到 - 看起来答案可能是* less * jQuery :) –

+0

你可以做到这一点你的方式,但你只需要将本地DOM元素推入数组,然后跳过'.join()'。如果jQuery收到一个数组,它会用它的内容填充jQuery对象。或者,您可以执行'var newOptions = $();',然后'newOptions.push(...)',但'.push()'不被官方支持。 – RightSaidFred

0

首先仔细检查一下,你是不是多次发出相同的AJAX请求。

一个快速简便的解决方案是将您的AJAX请求分成多个请求。首先将它分成两部分,如果还不够,则将它们除以3或更多。

然后,您可以检查哪个JSON大小适合您,并使用该大小。或者你可以让你的php根据总城市的数量来决定需要多少请求。

对于非常大的列表,我不得不将每个请求分成100个项目。第一个请求我会得到第一个列表以及一些数据,这些数据指示我为获得完整列表而必须提出多少请求,附加新节点,然后我将完成剩余请求,直到获得完整列表。

0

我不明白这会如何为您崩溃。在这里,我做了最坏的事情,它是速度极快:

http://jsfiddle.net/HZnYQ/

每次选择的东西时,我删除一个所有的元素之一,然后追加他们一个接一个,而它仍然时刻。其实我的CPU甚至没有记下它。