2

So thanks to some great feedback from my previous question, I have moved further: Original Post - Coldfusion Populate form with dropdown selection using ajax

So now, I have successfully submitted a request to my CFC and it has a remote function but I'm not getting anything returned to the browser.

The code looks like this:

<script>
function loadQuery() {
var assign = $("#existingAssignment").val(); // Get the value of the select box so we can build the next page

$.ajax({
    type: 'get',
    url: 'http://127.0.0.1/WMT/model/getCandidate.cfc', 
    data: {method:'getExistingAssignPosInfo', tourid : assign},
    dataType: 'json',
    async: false,
    success: function(result){
        var myDP4 = $('#dp4').val(result.STROTG);
        $('#dp4').val(result.STROTG);
        $('#dp5').val(result.STRDEPART);
    }
}); 
}
</script>

I am trying to have jQuery replace the results (which are coming back. I can see them in Firebug) into a form field. The fields are named dp4 and dp5 etc. I have tried setting a result to myDP4 and setting that value and it doesn't work.

I have also tried throwing an alert to the screen and that doesn't work as I assume it would put the alert on the ajax call.

The only other thing to do after filling in the form values would be to set two dropdowns on the screen to equal the number that is being passed back.

For example:

<option value="1">Name1</option>
<option value="2">Name2</option>
<option value="3">Name3</option>
<option value="4">Name4</option>

If my ajax returns 3 for the name of the select with the options, how would I send that info to jQuery so that it selects that in the drop down?

TIA!

UPDATE: one of the things that I added to my CFC was returnformat="JSON" that was not there before and I do see a JSON object when I call the CFC directly. However, there are no leading slashes as suggested below.

Thanks

Update #2:

This is the JSON I get back from calling the page directly

{
    "COLUMNS": [
        "STRPOSTITLE",
        "STROFFICE",
        "STROTG",
        "STRDEPART",
        "FDREDITASSIGN1STRNR_FROM",
        "FDREDITASSIGN1STRNR_TO",
        "FDREDITASSIGN2NDRNR_FROM",
        "FDREDITASSIGN2NDRNR_TO",
        "FDREDITASSIGN3RDRNR_FROM",
        "FDREDITASSIGN3RDRNR_TO",
        "FDREDITASSIGN4THRNR_FROM",
        "FDREDITASSIGN4THRNR_TO",
        "FTAEDITASSIGNTOURCOMMENTS",
        "FTAMISSIONEDITASSIGNTOURCOMMENTS"
    ],
    "DATA": [
        [
            1872,
            19,
            "February, 20 2013 00:00:00",
            "February, 19 2014 00:00:00",
            null,
            null,
            null,
            null,
            null,
            null,
            null,
            null,
            null,
            null
        ]
    ]
}

Update #3: Ok I'm able to fill in the data that I want but there is still a little more work to go. I have the data filling in the proper form fields but I need to Set the drop down to match what is returned. The query returns a number. That number will match the option value of a drop down. I have been trying to find it with:

$('#PosTitle').find('option:eq(PosVar)').prop('selected', true);

But that doesn't set the value.

Thanks again!

Community
  • 1
  • 1
weggie
  • 427
  • 1
  • 10
  • 26
  • Is your JSON response preceded by these two characters "//" (from what you see in Firebug)? See this post http://stackoverflow.com/questions/15501123/coldfusion-10-returnformat-json-adding-characters . – Miguel-F May 06 '13 at 19:30
  • Should I see the leading slashes? I am looking at the JSON that's spit out from calling the CFC directly and I don't see and //. Thanks! – weggie May 06 '13 at 19:48
  • For added security ColdFusion, by default, adds the characters "//" (the JavaScript comment character) to serialized JSON. When that happens you need to deal with those characters on the front-end before you can use the data. If you are not seeing the characters then this may not apply to your issue. – Miguel-F May 06 '13 at 19:53
  • Could you include an example of the JSON that is being returned from your CFC? – Miguel-F May 06 '13 at 20:01
  • I'll try to copy and paste a result soon. Thanks – weggie May 06 '13 at 20:20
  • Have you verified that your function works properly by calling it from coldfusion, and not javascript? – Dan Bracuk May 06 '13 at 21:31
  • {"COLUMNS":["STRPOSTITLE","STROFFICE","STROTG","STRDEPART","FDREDITASSIGN1STRNR_FROM","FDREDITASSIGN1STRNR_TO","FDREDITASSIGN2NDRNR_FROM","FDREDITASSIGN2NDRNR_TO","FDREDITASSIGN3RDRNR_FROM","FDREDITASSIGN3RDRNR_TO","FDREDITASSIGN4THRNR_FROM","FDREDITASSIGN4THRNR_TO","FTAEDITASSIGNTOURCOMMENTS","FTAMISSIONEDITASSIGNTOURCOMMENTS"],"DATA":[[1872,19,"February, 20 2013 00:00:00","February, 19 2014 00:00:00",null,null,null,null,null,null,null,null,null,null]]} – weggie May 07 '13 at 02:33
  • Yes, I can get this when I call it directly. This is the JSON returned. – weggie May 07 '13 at 02:34
  • Okay, so reading over your question again, are you saying that when you put an `alert(result);` in your `success:` function it is not showing anything? – Miguel-F May 07 '13 at 15:15
  • Correct: In the pop up I get: [object Object] – weggie May 07 '13 at 15:18
  • I think you would want to dump it out to see the content. I'd use this jQuery plugin for that: http://code.google.com/p/jquery-dump-plugin/ – Snipe656 May 07 '13 at 15:20
  • Ok.. going to try that and see what I can find. – weggie May 07 '13 at 15:23
  • What if you `alert(result.COLUMNS[0]);` and `alert(result.DATA[0]);`? – Miguel-F May 07 '13 at 15:26
  • `code`OBJECT = { COLUMNS : [ /*0*/ "STRPOSTITLE", /*1*/ "STROFFICE", /*2*/ "STROTG", /*3*/ "STRDEPART", /*4*/ "FDREDITASSIGN1STRNR_FROM", /*5*/ "FDREDITASSIGN1STRNR_TO", /*6*/ "FDREDITASSIGN2NDRNR_FROM", /*7*/ "FDREDITASSIGN2NDRNR_TO", /*8*/ "FDREDITASSIGN3RDRNR_FROM", /*9*/ "FDREDITASSIGN3RDRNR_TO", /*10*/ "FDREDITASSIGN4THRNR_FROM", /*11*/ "FDREDITASSIGN4THRNR_TO", /*12*/ "FTAEDITASSIGNTOURCOMMENTS", /*13*/ "FTAMISSIONEDITASSIGNTOURCOMMENTS" ], DATA : [ /*0*/ { ... } ] }; – weggie May 07 '13 at 15:28
  • It also has data and it has the same info that I see in the browser. When using alert(result.COLUMNS[0]); I get: STRPOSTITILE When using alert(result.DATA[0]); I get: 1328,29,October, 02 2010 00:00:00,January, 26 2011 00:00:00,,,,,,,,,, – weggie May 07 '13 at 15:31

1 Answers1

0

Based on what I'm seeing from the comments, you are not serializing your JSON correctly. Look at Adobe's documentation for encode to ensure you have a JSON-formatted string to pass to your AJAX request.

acconrad
  • 3,201
  • 1
  • 22
  • 31
  • Not sure which version you saw, but as of "update #2" it is serialized correctly. CF's default representation of query objects is just a little wacky ;-) – Leigh May 09 '13 at 02:36