0

In my backbone project I am trying to fetch a model based on some search criteria submitted by the users from a form. In submit handler, I am trying to fetch the model by passing search criteria's via data option (following is the code).

var productType=$("#productType").val();
var customerId=$("#customerId").val();
var stateSelected=$("#selectedState").val();
var srbStatus=$("#stateReportingStatus").val();
var dateType=$("#dateType").val();
var fromDate=$("#fromDate").val();
var toDate=$("#toDate").val();
var billTypeInd=$("#billTypeIndicator").val();
var dataElement=$("#dataElement").val();
var ediFileName=$("#ediFileName").val();
var ediBillName=$("#ediBillNumber").val();
var billId=$("#billId").val();
var claimantFirstName=$("#claimantFirstName").val();
var claimantLastName=$("#claimantLastName").val();
var insurerName=$("#insurerName").val();
var insurerFEIN=$("#insurerFEIN").val();
var insurerZip=$("#insurerZIP").val();
var dashboardSearchResultModel= new dashboardSearchResult();
var dashboardSearchResultModel= new dashboardSearchResult();
dashboardSearchResultModel.fetch({
            data:{
                productType: productType,
                customerId: customerId,
                state:stateSelected,
                srbStatus:srbStatus,
                dateType:dateType,
                fromDate:fromDate,
                toDate:toDate,
                billTypeInd:billTypeInd,
                dataElement:dataElement,
                ediFileName:ediFileName,
                ediBillName:ediBillName,
                billId:billId,
                claimantFirstName:claimantFirstName,
                claimantLastName:claimantLastName,
                insurerName:insurerName,
                insurerFEIN:insurerFEIN,
                insurerZip:insurerZip
            },
            wait:true,
            success: function(dashboardSearchResultModel)
            {        
                alert("This is what we get for     result"+JSON.stringify(dashboardSearchResultModel));
                $('#dashboardResultArea').html(self.dashboardResultTemplate({results:dashboardSearchResultModel.get("results")}));
            },
            error: function (model, xhr, options) {
                alert("Error: An error occurred while trying to fetch the dashboardSearchResultModel");
                alert("Error got model"+JSON.stringify(model));
                alert("options:"+JSON.stringify(options));
                alert("xhr:"+JSON.stringify(xhr));
            }
        });

Initially when the page loads after providing the search criteria's if I click submit the fetch doesn't work and goes to the error handler. After that when I submit the from second time the fetch works and retrieves data from the backend server. Any idea what is wrong? Thanks in advance.

  • What error are you getting? – David Sulc Jan 28 '14 at 07:20
  • first Error: An error occurred while trying to fetch the dashboardSearchResultModel – Nayeem Md. Quayum Jan 28 '14 at 07:27
  • then: Error got model{}, then:options:{"data":{"productType":"all","customerId":"all","state":"all","srbStatus":"1","dateType":"Blank","fromDate":"","toDate":"","billTypeInd":"2","dataElement":"","ediFileName":"","ediBillName":"","billId":"","claimantFirstName":"","claimantLastName":"","insurerName":"","insurerFEIN":"","insurerZip":""},"wait":true,"parse":true,"emulateHTTP":false,"emulateJSON":false,"xhr":{"readyState":0,"responseText":"","status":0,"statusText":"error"}}, finally: xhr:{"readyState":0,"responseText":"","status":0,"statusText":"error"} – Nayeem Md. Quayum Jan 28 '14 at 07:29
  • You're receiving a status "0" as a response, it looks like the ajax call deosn't even get to your server. See http://stackoverflow.com/questions/3825581/does-an-http-status-code-of-0-have-any-meaning Also check your server logs for issues. – David Sulc Jan 28 '14 at 07:31

3 Answers3

0

When error callback is called, it is because your XHTMLRequest to the server returned a error (HTTP status code). So, there is where your problem resides.

Who starts this code? As the erros does not occur on a second attempt, I would suggest that you area callind $('#id').val() when the DOM is not ready. This way you are sending null values to the server, and that's causing the error you are receiving.

To solve your problem, assure you DOM is ready when executing this script. See if your request is leaving the browser and reaching the server (i.e., cross-domain request fail with status 0, not reaching the server).

And, if it is, debug your server-side, as it does not seem to be an client-side problem.

Walter Macambira
  • 2,574
  • 19
  • 28
  • Thanks Walter for your input. I am calling the this particular view's render function from the router, and in the render method populating the input form. When the submit button is clicked, from the handler method associated with the submit button the above mentioned code is executing. If I print out each input (for example alert($('#id').val())) it prints out the value, therefor I am assuming the dom is ready and I can collect the input data from the form. When I checked whether the request left the browser I found the status for the request is cancelled. Any idea what is going on? – Nayeem Md. Quayum Jan 28 '14 at 16:08
  • Requests may get canceled,as I and David Sulc said, due to cross domain request. The URL you are calling and you application are at the same domain? Would you mind adding it to your question? – Walter Macambira Jan 28 '14 at 18:29
  • Yes both the backend spring controllers and backbone application is in same domain. If I try the URL from browser it works and returns the right response. The url I am trying is Request URL:http://localhost:8080/hes_srb_webapp/rest/dashboardResult?productType=all&customerId=all&state=all&srbStatus=16&dateType=Date+Reported&fromDate=12%2F01%2F2013&toDate=12%2F01%2F2013&billTypeInd=2&dataElement=dataelement&ediFileName=edifile&ediBillName=edi1&billId=bill1&claimantFirstName=first&claimantLastName=second&insurerName=insurer&insurerFEIN=fein&insurerZip=12345 – Nayeem Md. Quayum Jan 28 '14 at 19:23
  • This data is not being set to your HTTP request. Can you show me the HTTP GET request? – Walter Macambira Jan 28 '14 at 19:59
  • Request URL:http://localhost:8080/hes_srb_webapp/rest/dashboardResult?productType=all&customerId=all&state=all&srbStatus=16&dateType=Date+Reported&fromDate=12%2F01%2F2013&toDate=12%2F01%2F2013&billTypeInd=2&dataElement=data&ediFileName=edi&ediBillName=edi1&billId=bill1&claimantFirstName=first&claimantLastName=last&insurerName=insurer&insurerFEIN=fein&insurerZip=12345 – Nayeem Md. Quayum Jan 28 '14 at 21:59
  • Request Headers CAUTION: Provisional headers are shown. . Accept:application/json, text/javascript, */*; q=0.01 . Referer:http://localhost:8080/hes_srb_webapp/dashboard.html . User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.77 Safari/537.36 . X-Requested-With:XMLHttpRequest – Nayeem Md. Quayum Jan 28 '14 at 22:01
  • Query String Parametersview sourceview URL encoded . productType:all . customerId:all . state:all . srbStatus:16 . dateType:Date Reported . fromDate:12/01/2013 . toDate:12/01/2013 . billTypeInd:2 . dataElement:data . ediFileName:edi . ediBillName:edi1 . billId:bill1 . claimantFirstName:first . claimantLastName:last . insurerName:insurer . insurerFEIN:fein . insurerZip:12345 Walter here is the details HTTP GET request – Nayeem Md. Quayum Jan 28 '14 at 22:02
  • Sorry, but I can't see where is the problem. Consider trying to make a "get" using $.ajax, and see if everything works fine. – Walter Macambira Jan 29 '14 at 20:23
  • Walter I was also considering that. Can you suggest a good tutorial or documentation on how to use $.ajax to get data from backend. Thanks. – Nayeem Md. Quayum Jan 30 '14 at 04:08
  • Thanks a bunch for the suggestion Walter. Do you think this is the only issue? – Nayeem Md. Quayum Jan 31 '14 at 00:29
  • Your problem is with the XHTMLRequest. Compare both requests, the one that throws the error and the one that succeeds. See if htere is any difference. – Walter Macambira Jan 31 '14 at 12:59
  • Hei Walter, I am struggling to find exact method to achieve the above using $.ajax. I would really appreciate if you could suggest a solution for above problem using $.ajax call. Another thing is I came across to a command called Backbone.ajax. Is it same as $.ajax call? Thanks. – Nayeem Md. Quayum Feb 03 '14 at 09:04
  • Yes. See Backbone documentation, it uses $.ajax, but throught its Backbone.sync implementation. – Walter Macambira Feb 03 '14 at 16:31
0

So after trying many things I finally decided to try $.ajax call rather the fetch method. This is what I came up with

$.ajax({
              type: "GET",
              url: "rest/dashboardResult",
              dataTyp: 'json',
              data: { 
                productType: productType,
                customerId: customerId,
                state:stateSelected,
                srbStatus:srbStatus,
                dateType:dateType,
                fromDate:fromDate,
                toDate:toDate,
                billTypeInd:billTypeInd,
                dataElement:dataElement,
                ediFileName:ediFileName,
                ediBillName:ediBillName,
                billId:billId,
                claimantFirstName:claimantFirstName,
                claimantLastName:claimantLastName,
                insurerName:insurerName,
                insurerFEIN:insurerFEIN,
                insurerZip:insurerZip
              }
            })
              .done(function(response) {
                alert( "Result is: " + response);

        });

This works without any problem from the get go. Now my question is how to bind the response to the backbone model?

0

Finally I figured out what was wrong. The call was inside a submit click handler, and $.ajax call or fetch is asynchronous. So by the time the call got reply from the server default action of submit click already took place (which is to reload the page). So by the time success or .done got called the whole page was reloaded. So I put event.preventDefault() at the beginning of handler method and let the handler receive the call back from the server and display it at the template. Thanks everyone for your time.