0

I have asp.net mvc web api 2 url. I need to use that web API data integrate to PHP application search and data(postcode display list) auto complete. These below my jQuery and PHP search page code, but it is not working for me. Please tell me why was that?

This is asp.net mvc web api 2 JSON return values.

[{"ID":6,"SuburbName":"Carlton North","postcode":"3054","Territory":"MEL-Brunswick","Latitude":-  37.7845911,"Longitude":144.972883,"AuState":"VIC","created":"13/03/2015 12:00:00 AM","updated":"13/03/2015 12:00:00 AM"}
[{"ID":7,"SuburbName":"Carlton South","postcode":"3054","Territory":"MEL-Brunswick","Latitude":-    37.7845911,"Longitude":144.972883,"AuState":"VIC","created":"13/03/2015 12:00:00 AM","updated":"13/03/2015 12:00:00 AM"}

This is my jQuery code

var searchRequest = null;
$(".auto").autocomplete({
    maxLength: 5,
    source: function(request, response) {
        if (searchRequest !== null) {
            searchRequest.abort();
        }
        searchRequest = $.ajax({
            url: 'asp.net mvc api web url',
            method: 'post',
            dataType: "json",
            data: {term: request.term},
            success: function(data) {
                searchRequest = null;
                response($.map(data.items, function(item) {
                    return {
                        value: item.SuburbName,
                        label: item.SuburbName
                    };
                }));
            }
        }).fail(function() {
            searchRequest = null;
        });
    }
});
<form method="POST" action="search.php">
  Enter your Postcode: 
  <input type="text" name="search" >
  <input name="submit" type="submit" class="auto" value="<?php echo $_POST['search'];?>" /><br />
</form>
if(isset($_POST['submit']))
{
  $value=$_POST['search'];
}
Artjom B.
  • 61,146
  • 24
  • 125
  • 222
Rob
  • 193
  • 5
  • 18
  • The "Run code snippet" didn't seem to work. What exception you're getting? – mike123 Mar 14 '15 at 02:53
  • I am not getting any data form that code – Rob Mar 14 '15 at 03:02
  • if you inspect the response from the server using either Chrome or Fiddler do you see the data? – mike123 Mar 14 '15 at 03:09
  • Only I can see data if I use asp.net mvc web api 2 url it'll display data – Rob Mar 14 '15 at 03:14
  • When I run PHP code it'll come only JSON 1 (null) 0 – Rob Mar 14 '15 at 03:30
  • Hello, Please tell me why is not print $value in here $json = file_get_contents('http://www.wwwcc.com.au/api/api/values?pcode=. $value .'); if I use echo $value somewhere else it display value. – Rob Mar 14 '15 at 08:32
  • Rob you may be on to something. Try either of these two: $json = file_get_contents('wwwcc.com.au/api/api/values?pcode='.$value.'') or $json = file_get_contents('wwwcc.com.au/api/api/values?pcode=$value') – mike123 Mar 14 '15 at 14:02
  • In your above example it seem like you are missing closing single comma – mike123 Mar 14 '15 at 14:04
  • Thank you for you help, This $json line is working now. – Rob Mar 14 '15 at 23:30
  • Hello Mike, $value=$_POST['search']; $json = file_get_contents('http://wwwcc.com.au/api/api/values?pcode='.$value.''); now $value come, but stiil jQuery autocomplete not working. What do I have to do? – Rob Mar 15 '15 at 00:18
  • I would try to simplify the autocomplete configuration - try this: `$( "#postcode" ).autocomplete({ source: "asp.net mvc api web url" }) .autocomplete( "instance" )._renderItem = function( ul, item ) { return $( "
  • " ) .append( "" + item.SuburbName + "
    " + item.SuburbName + "
    " ) .appendTo( ul ); };`
  • – mike123 Mar 15 '15 at 00:37
  • Humm ... not sure if it's any simple. But see if that works. – mike123 Mar 15 '15 at 00:42
  • Hello Mike, I have already hard coded pcode=3023 in my below code. That number has 3 records, but display now "No search results" where does come from "No search results" ? – Rob Mar 15 '15 at 02:53
  • $( '.auto' ).autocomplete({ source: 'http://www.wwwc.com.au/api/api/values?pcode=3023' } ) .autocomplete( "instance" )._renderItem = function( ul, item ) { return $( "
  • " ) .append( "" + item.SuburbName + "
    " + item.SuburbName + "
    " ) .appendTo( ul ); };autocomplete( "instance" )._renderItem = function( ul, item ) { return $( "
  • " ) .append( "" + item.SuburbName + "
    " + item.SuburbName + "
    " ) .appendTo( ul ); };
  • – Rob Mar 15 '15 at 02:53
  • Rob, one other thing ... you may have revised your mark-up but I don't see the element with an id of postalcode `
    Enter your Postcode:
    `; this element should prob have it ``
    – mike123 Mar 15 '15 at 03:02
  • Rob, the "No search result" may be coming back from web api, When you trace the data from web api what you see? – mike123 Mar 15 '15 at 03:09
  • Hello Mile, I tried both. but not working for me – Rob Mar 15 '15 at 03:26
  • web API pcode=3023'; has 3 values. it can't be web API – Rob Mar 15 '15 at 03:28
  • I have already update my question below code – Rob Mar 15 '15 at 03:33
  • Can you you please explain to me ""instance" autocomplete( "instance" ) – Rob Mar 15 '15 at 03:35
  • Hello Mike, I think this case we have to use jQuery Autocomplete before search(Post)? – Rob Mar 15 '15 at 06:48
  • I check Fiddler, GET /api/api/values?pcode=3043 HTTP/1.1 no any values return. only valve come post and submit
    What is the solution for auto complete data display before press submit button?
    – Rob Mar 15 '15 at 12:43
  • Hi Rob, i'll try to replicate your test case and see more what may be the issue – mike123 Mar 15 '15 at 15:29
  • Rob i have created the jsfiddler, check it out: http://jsfiddle.net/mike_123/DLLVw/726/ – mike123 Mar 15 '15 at 19:32
  • Hello Mike, thank you for your code and your time. please let me know that jQuery library requirement is only these two? – Rob Mar 16 '15 at 05:03
  • Hello Mike, I have already copy and past your all code I put my test server but it did not work http://www.naturalbluesapphires.com/rob/test.php. Also I added another jquery code and it'll work http://www.naturalbluesapphires.com/rob/test1.php, Why was not working test.php page? – Rob Mar 16 '15 at 08:44
  • You need to update the url and data properties: $.ajax({ url: "asp.net mvc api web url", dataType: "json", data: {term: request.term}, type:"POST", – mike123 Mar 16 '15 at 13:59
  • Also a couple of things to check: 1) the webapi does it live on the same server? if not you need to adjust the settings for cross-domain calls (see here http://jqueryui.com/autocomplete/#remote-jsonp); 2) make sure parameter **term** matches the signature of the webapi (postcode???) – mike123 Mar 16 '15 at 14:02
  • Hello Mike, according to you instruction I did changers but It did not work for me. herewith I am sending you my test URL, you can see source code under view source http://www.naturalbluesapphires.com/rob/auto1.php. The page top PHP code included If you want to test use jqery url: $json, or direct API url. – Rob Mar 17 '15 at 07:51
  • Rob, you need to wrap the script into document ready, like this: `$(document).ready(function(){ $(".auto").autocomplete({ ... }); ` – mike123 Mar 17 '15 at 14:05
  • also you've left the data: json (there are two of them, comment the first one). See what you get ... – mike123 Mar 17 '15 at 14:05
  • Hello mike, I have already updated my page http://www.naturalbluesapphires.com/rob/auto1.php still same issue. please refer my source code under page view source. – Rob Mar 17 '15 at 22:30
  • Hello mike, I have copy your http://jsfiddle.net/mike_123/DLLVw/726/ jquery one to one code and paste my test server please use this url http://www.naturalbluesapphires.com/rob/auto2.php. but it did not work. also please check what I am using library use view source code. – Rob Mar 18 '15 at 09:09
  • Helllo mike, this url http://www.naturalbluesapphires.com/rob/auto3.php when you check Fiddler Json valve return. but not display in the the page, why was that? – Rob Mar 18 '15 at 11:17
  • Hi Rob, i tried to replicate the problem using the web api url in your example, and I'm running into exactly the same issue where webapi returns data, but autocomplete doesn't pick it up. I tried to use "json**p**", callbacks, etc. - it just wouldn't work. Let me play with it some more and get back to you ... – mike123 Mar 19 '15 at 17:58
  • Rob, just thought i'd follow up bellow with a properly formatted feedback. See if that helps. – mike123 Mar 20 '15 at 18:07