locked
Issues connecting the search contract to my database through a web-service RRS feed

  • Question

  • Hi,

    I have tried reading lots of the search contract documentation but am still having issues.  I have added the search contract to my application (based on the blank template) and I am now trying to have the search contract connect to my database so that I can query the database using the users search term and return a result.

    I have had some success by call the Win.JS function in the OnSubmitted function I can connect to my web-service, query my database and retrieve the results HOWEVER I can only do this when I hard code the Win.JS url. I need help replacing this hardcoded url with one that dynamically incorporates the users search term. As you will see below I have hardcoded the variable searchString, however ideally I want this to equal the search term entered by the user. How do I do this?

    // How search performs when the app is the MainApp
        appModel.Search.SearchPane.getForCurrentView().onquerysubmitted = function (args) {
            
            var testTerm = document.getElementById("definition");
            var testDef = document.getElementById("description");
    
            var searchString = 2;
            var searchFormat = 'JSON';
                     
            var searchurl = 'http://www.xxxx.com/web-service.php?termID=' + searchString +'&format='+searchFormat;
            
            WinJS.xhr({url: searchurl})
              .done(function fulfilled(result)
    
              {
                  //Show Terms                 
                  var searchTerm = JSON.parse(result.responseText);
                                
                  // var terms is the key of the object (terms)
                  // on each iteration of the loop the var terms is assigned the name of the  object key
                  // and the if stament is evaluated
    
                  for (terms in searchTerm) {
                      
                      //terms will find key "terms"
                      var termName = searchTerm.terms[0].term.termName;
                      var termdefinition = searchTerm.terms[0].term.definition;
                      
                      //WinJS.Binding.processAll(termDef, termdefinition);
                      testTerm.innerText = termName;
                      testDef.innerText = termdefinition;
                  }
                  
            },
    
                      function error(result) {
                          testDef.innerHTML = "Got Error: " + result.statusText;
                      },
    
                      function progress(result) {
                          testDef.innerText = "Ready state is " + result.readyState;
                      });
            
    
            //navigate(searchPageURI);
            //print
    
        };

    Also, are there any examples of this, all MSFT examples seem to use generic data and do not connect to any database?

    Thanks,


    • Edited by Maracles Monday, December 3, 2012 8:26 PM
    Monday, December 3, 2012 8:25 PM

Answers

  • Hi,

    Please refer to the example as follow:

     function CallData() {
            var person = { 'Name': 'Name'};
            WinJS.xhr({
                type: "POST",
                url: "http://www.xxx.com/WebService.asmx/method",
                headers: { "Content-type": "application/json" },
                data: JSON.stringify(person)
            }).then(function complete(request) {
                var resdata = request.responseText;
            }, function error(er) {
                var err = er.statusText;
            });
        }


    Roy
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Maracles Friday, November 7, 2014 5:43 PM
    Tuesday, December 4, 2012 3:30 AM

All replies

  • Hi,

    Please refer to the example as follow:

     function CallData() {
            var person = { 'Name': 'Name'};
            WinJS.xhr({
                type: "POST",
                url: "http://www.xxx.com/WebService.asmx/method",
                headers: { "Content-type": "application/json" },
                data: JSON.stringify(person)
            }).then(function complete(request) {
                var resdata = request.responseText;
            }, function error(er) {
                var err = er.statusText;
            });
        }


    Roy
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Maracles Friday, November 7, 2014 5:43 PM
    Tuesday, December 4, 2012 3:30 AM
  • Hi Roy,

    Thanks for your reply, it looks like it does what I want but I have to say I don't fully understand it.  I'm very new to JS and this is my first attempt at a App.

    Firstly, where does this go? Does it replace the _searchData function in searchResults.js?

    Secondly, my web-service has been done with PHP and javascript, does the function you've provided still apply, I've noticed you're web service url is .asmx.

    Third, if the response from my Web-service is already JSON, do I need data.JSON.stringify?

    I may have other questions however I'm just trying to understand the basics of what you've posted first.

    Thanks,

    James 

    Tuesday, December 4, 2012 9:17 AM
  • Hi,

    First, the code above is how to call data from web service with WinJs.xhr, you could put that in your code where need winjs.xhr.

    Second, no matter php or asp.net, the web service will be ok. You could use fiddler to check whether get data correctly.

    Third, JSON.stringify just convert json to string. So you don't have to do that, unless there need.


    Roy
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Song Tian Friday, December 7, 2012 8:47 AM
    • Unmarked as answer by Maracles Friday, December 7, 2012 4:03 PM
    Wednesday, December 5, 2012 2:25 AM
  • I still cannot see which part of the code, or which variable, refers to the actual search term?

    Friday, December 7, 2012 4:04 PM