0

I am creating a "Garment checker" using ajax and I would like the user to put the ID into the input followed by a request to the URL. which prints out the code if it exists. The code below just isn't doing the job as I would like it to although I think that it is almost correct. Can anybody see my mistakes or point me in the right direction please?

<script type="text/javascript">
  //<![CDATA[
  $(document).ready(function() {
    "use strict";


    $('#lookupForm')
      .removeAttr('onsubmit')
      .submit(function(event) {
        event.preventDefault();

        var target = document.getElementById('garmentID');
        if(target.value.length > 0) {
          fetchData(target.value);
        }
      });
    });

        function fetchData(garmentID) {
          var url = 'http://staging.me-tail.net/api/3.0/retailer/4/garmentsAvailable?guid=' + applicationID;

          $.getJSON(url, function(data) {

            var appDetails = data.AvailableSkus[0];
            $('#garmentTitle').val(appDetails.AvailableSkus);
      });
        }
  //]]>
</script>
2
  • 1
    What is your question and what isn't working? Commented Sep 27, 2013 at 11:07
  • is "applicationID" defined in the url ? Commented Sep 27, 2013 at 11:55

2 Answers 2

1

Since data.AvailableSkus seems to be an array, you don't want to pass the collection as the value to #garmentTitle.

You're most likely after a property (if the array contains objects) or the actual element:

//if typeof appDetails.AvailableSkus[0] is string or number:
$('#garmentTitle').val(appDetails.AvailableSkus[0]); 

or

//if typeof appDetails.AvailableSkus[0] is an object:
$('#garmentTitle').val(appDetails.AvailableSkus[0].someProp);

value Type: String or Array A string of text or an array of strings corresponding to the value of each matched element to set as selected/checked.

Sign up to request clarification or add additional context in comments.

4 Comments

Still not getting an output. This is as far as I have got.jsfiddle.net/Lxbb9/3
@user2822683 THe issue seems to be that it's a cross domain call.
DO you know what I am missing here?
@user2822683 What serverside language are you using?
0

This link provides output properly

http://staging.me-tail.net/api/3.0/retailer/4/garmentsAvailable?guid=5

are you sure "applicationID" is setted before ?

var url = 'http://staging.me-tail.net/api/3.0/retailer/4/garmentsAvailable?guid=' + applicationID;

Any error message in firebug console ?

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.