0

Here is one that's been driving us a little insane. We implemented the example script for JSONP from the jQuery UI page http://jqueryui.com/autocomplete/#remote-jsonp then right in place next to it, we wrote a minimally modified version.

Here's the kicker. The script from jQuery ui works in IE and ours only works in Firefox, Chrome, etc

The ONLY difference is our datasource.

It there some magic thing we need to do to our data when we send it to the client? We've tried wrapping it in named functions, etc.

<!doctype html>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta http-equiv="X-UA-Compatible" content="chrome=1">
    <title></title>
    <link rel="stylesheet" href="/css/jquery-ui.css" />
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.9.1/jquery-ui.min.js"></script>
</head>

<body>
    <script>
        function foo (str) {
            //console.log(str);
            return str;
        }
        $(function() {
            function log( message ) {
                $( "#street1" ).val( message );
            }
            $( "#street1" ).autocomplete({
                source: function( request, response ) {
                $.ajax({
                    url: "http://192.168.50.78/melissa/address/linux/interfaces/php/api/autoAddrSuggest_new_dual2.php",
                    dataType: "json",
                    data: {
                        format: 'json',
                        action: 'address1request',
                        zipcode: '43026',
                        address: request.term
                    },
                    success: function( data ) {                        
                        console.log( data.addresses );
                        response( $.map( data.addresses, function( item ) {
                            return {                                
                                label: item.address,
                                value: item.address
                            };
                        }));                    
                    },
                    error: function(data) {
                        console.log(data);
                    }   
                });
            },
            minLength: 2,
            select: function( event, ui ) {
                log( ui.item ?
                    "Selected: " + ui.item.value :
                    "Nothing selected, input was " + this.value);
            },
            open: function() {
                $( this ).removeClass( "ui-corner-all" ).addClass( "ui-corner-top" );
            },
            close: function() {
                $( this ).removeClass( "ui-corner-top" ).addClass( "ui-corner-all" );
            }
        });

        $( "#city" ).autocomplete({
            source: function( request, response ) {
                $.ajax({
                    url: "http://ws.geonames.org/searchJSON",
                    dataType: "jsonp",
                    data: {
                        featureClass: "P",
                        style: "full",
                        maxRows: 12,
                        name_startsWith: request.term
                    },
                    success: function( data ) { 
                        console.log( data.geonames );                       
                        response( $.map( data.geonames, function( item ) {                            
                            return {                                
                                label: item.name + (item.adminName1 ? ", " + item.adminName1 : "") + ", " + item.countryName,                                
                                value: item.name                            
                            };
                        }));                    
                    }                
                });            
            },            
            minLength: 2,            
            select: function( event, ui ) {                
                log( ui.item ?                    
                    "Selected: " + ui.item.label :                    
                    "Nothing selected, input was " + this.value);            
            },            
            open: function() {                
                $( this ).removeClass( "ui-corner-all" ).addClass( "ui-corner-top" );            
            },            
            close: function() {                
                $( this ).removeClass( "ui-corner-top" ).addClass( "ui-corner-all" );            
            }        
    });
    });         


</script>
<div id="wrap">
    <div id="main">
        <div>
            <form id="enroll" name="enroll" method="post" enctype="multipart/form-data">

            <div>

                <p class="left">
                    <span>&nbsp;</span><br/>
                    <input id="street1" />
                    <input id="city" />
                </p>
            </div>

            </form>
        </div>
     </div>
  </div>

</body>
</html>

For giggles, here is the output from the working json service

{"totalResultsCount":12958,"geonames":[{"countryName":"Russia","adminCode1":"33","fclName":"city, village,...","countryCode":"RU","lng":51.4913,"fcodeName":"seat of a second-order administrative division","toponymName":"Uni","fcl":"P","name":"Uni","fcode":"PPLA2","geonameId":478996,"lat":57.751,"adminName1":"Kirov","population":4767},{"countryName":"Russia","adminCode1":"54","fclName":"city, village,...","countryCode":"RU","lng":72.622724,"fcodeName":"populated place","toponymName":"Uki","fcl":"P","name":"Uni","fcode":"PPL","geonameId":1488608,"lat":56.991135,"adminName1":"Omsk","population":0},{"countryName":"Philippines","adminCode1":"07","fclName":"city, village,...","countryCode":"PH","lng":124.3252,"fcodeName":"populated place","toponymName":"Union","fcl":"P","name":"Union","fcode":"PPL","geonameId":1680262,"lat":10.6695,"adminName1":"Central Visayas","population":4581},{"countryName":"Philippines","adminCode1":"13","fclName":"city, village,...","countryCode":"PH","lng":126.1102778,"fcodeName":"populated place","toponymName":"Union","fcl":"P","name":"Union","fcode":"PPL","geonameId":1680267,"lat":9.7566667,"adminName1":"Caraga","population":2368}]}

And here is the output from our server

{"addresses":[{"address":"3666 LACEY WOODS PARK"},{"address":"3666 LACON RD"},{"address":"3666 LAKESTONE CIR"},{"address":"3666 E LINKS CIR"}]}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • You do need to send back the response wrapped in a callback function. You will need to read the `callback` (or similar) parameter and wrap the response in a call to that function. – Andrew Whitaker Nov 19 '12 at 18:19
  • Could you provide an example? What precisely should our API respond to the client with? you mean the response from the api should be: "myfunctionname( ...json...)" instead of just "json" – Daniel Saint James Nov 19 '12 at 20:34
  • Well maybe I should back up and say that you don't need JSONP if you are making a request in the same domain as the page was served up from. Is this the case? Actually it doesn't look like you're using JSONP in your code. Do you get any errors on the page at all? – Andrew Whitaker Nov 20 '12 at 03:49

2 Answers2

0

JSONP protocol is a cross site scripting adhoc protocol. It is needed for cases when domain that you query and domain which was used to download the page are 2 different domains. I guess your problem is that the browser is not allowing you do a direct request request to 192.168.50.78 , since your original domain is something else. Note the difference you are using json, and remote-jsonp uses jsonp.

Brad Larson
  • 170,088
  • 45
  • 397
  • 571
Night Warrier
  • 381
  • 4
  • 12
  • Do you mean that IE isn't allowing it? And again, both requests are being made to other domains, and yet one works and one does not. – Daniel Saint James Nov 19 '12 at 20:09
  • Yes, browsers (disregarding of whether it is IE or Safari or Chrome) disallow direct AJAX requests to a different than the origin domains. The fact that remote-jsonp example uses jsonp scheme is decisive in this case, and this is why it works. You have to implement jsonp endpoint in your service and do the request using jsonp. See JQuery jsonp for more information on how to do this. There is another technique which can be also applied here CORS : http://en.wikipedia.org/wiki/Cross-origin_resource_sharing – Night Warrier Nov 19 '12 at 22:19
  • Strange then because in it's JSON version, it works in every browser EXCEPT IE. JSONP needs a wrapper with a callback name, but I'm not quite able to wrap my brain around it. – Daniel Saint James Nov 19 '12 at 22:23
  • Which version of IE are you testing against? – Night Warrier Nov 19 '12 at 22:26
  • 8 and 9. The native json works perfectly in every browser but IE 8 or 9. Then nothing. – Daniel Saint James Nov 19 '12 at 22:36
0

console.log doesn't work in IE unless you open developer tools or implement a workaround:

What happened to console.log in IE8?

Take out or comment out all the console.log references OR hit F12 OR use a workaround and try it again in IE.

F12 in IE will reveal any js errors and is very useful for solving issues like this one even if it's not the console.log statements causing the issue.

Community
  • 1
  • 1
jk.
  • 14,365
  • 4
  • 43
  • 58