SitePoint Sponsor

User Tag List

Results 1 to 15 of 15
  1. #1
    SitePoint Wizard
    Join Date
    May 2012
    Posts
    1,157
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Post I don't know how to access this.

    Hi, I need your help please...I don't know how to acces this in jquery i want to get the id which is 001,but i get an error "JSON.parse: unexpected non-whitespace character after JSON data"

    This is the return in my firebug {"id":"001"}

    Code:
     $.ajax({
        type: 'post',
        data: {officeid:id},
        url: 'toOtherpage.cfm',
        success:function(data){
          var dtaobj = jQuery.parseJSON(data);
          console.log(dtaobj.id);
        }

    Thank you in advance.

  2. #2
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    6,051
    Mentioned
    219 Post(s)
    Tagged
    12 Thread(s)
    Hi jemz,

    Your syntax is correct.
    Can you log data to the console and post the output here?

    Code:
    $.ajax({
        type: 'post',
        data: {officeid:id},
        url: 'toOtherpage.cfm',
        success:function(data){
          console.log(data);
        }
    });

  3. #3
    SitePoint Wizard
    Join Date
    May 2012
    Posts
    1,157
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Pullo View Post
    Hi jemz,

    Your syntax is correct.
    Can you log data to the console and post the output here?

    Code:
    $.ajax({
        type: 'post',
        data: {officeid:id},
        url: 'toOtherpage.cfm',
        success:function(data){
          console.log(data);
        }
    });

    Hi Pullo,

    Code:
    $.ajax({
        type: 'post',
        data: {officeid:id},
        url: 'toOtherpage.cfm',
        success:function(data){
          console.log("data=",data);
        }
    });
    [/QUOTE]

    This is the output

    data={"id":"001"}

  4. #4
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    6,051
    Mentioned
    219 Post(s)
    Tagged
    12 Thread(s)
    Sorry, I should have seen it before.
    $.parseJSON takes a JSON string and returns the resulting JavaScript object.
    This means that you need to do this:

    Code:
    var dtaobj = $.parseJSON('{"id":"001"}');
    console.log(dtaobj.id);
    => 001
    or better still:

    Code:
    $.ajax({
        type: 'post',
        data: {officeid:id},
        url: 'toOtherpage.cfm',
        success:function(data){
          console.log(data.id);
        }
    });

  5. #5
    SitePoint Wizard
    Join Date
    May 2012
    Posts
    1,157
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Hi pullo,

    it will print undefined

  6. #6
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    6,051
    Mentioned
    219 Post(s)
    Tagged
    12 Thread(s)
    How are you returning the data from your cfm file?

  7. #7
    SitePoint Wizard
    Join Date
    May 2012
    Posts
    1,157
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Pullo View Post
    How are you returning the data from your cfm file?
    This is in my
    toOtherpage.cfm


    Code:
     <cfif IsDefined("officeid")>
             <cfset  mydata = officeId(#officeid#)>
             <cfoutput>#mydata#</cfoutput>
      </cfif>
       
         
     
    
     <cffunction name="officeId"> 
           <cfargument name="officeid">
             <cfquery  name="officequery" datasource="#mydatasource#"> 
                   Select officeid from office_tbl where officeid = '#officeid#' 
             </cfquery> 
                
                <cfset theStruct = StructNew()>   
                <cfset theStruct.officeid=#officeQuery.officeid#>
            <cfreturn   SerializeJSON(theStruct)> 
      </cffunction>

  8. #8
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    6,051
    Mentioned
    219 Post(s)
    Tagged
    12 Thread(s)
    So you seem to be returning JSON with the call to SerializeJSON

    If you add this to your success callback, what is output?

    Code:
    console.log(typeof data);

  9. #9
    SitePoint Wizard
    Join Date
    May 2012
    Posts
    1,157
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Pullo View Post
    So you seem to be returning JSON with the call to SerializeJSON

    If you add this to your success callback, what is output?

    Code:
    console.log(typeof data);
    it will print

    string

  10. #10
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    6,051
    Mentioned
    219 Post(s)
    Tagged
    12 Thread(s)
    Try setting the data type to JSON

    Code:
    $.ajax({
        type: 'post',
        data: {officeid:id},
        dataType: 'json',
        url: 'toOtherpage.cfm',
        success:function(data){
          console.log(typeof data);
        }
    });
    What happens then?

  11. #11
    SitePoint Wizard
    Join Date
    May 2012
    Posts
    1,157
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    still it will print string

  12. #12
    SitePoint Wizard
    Join Date
    May 2012
    Posts
    1,157
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    I don't understand why i could not get the office id,but the response return in json format

  13. #13
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    6,051
    Mentioned
    219 Post(s)
    Tagged
    12 Thread(s)
    According to the docs, serializeJSON returns a string that contains a JSON representation of the parameter value.

    This makes sense, as typeof(data) returns "String", but unfortunately, for whatever reason, $.parseJSON is choking and dying.

    I guess that there's something about the string that it cannot handle, as this:

    Code:
    var dtaobj = $.parseJSON('{"id":"001"}');
    console.log(dtaobj.id);
    => 001
    works as expected.

    Anyway, next idea, could you not have ColdFusion serialize the query object to JSON?

    Code:
    <cffunction name="officeId"  access="remote" returnType="query" returnFormat="JSON"> 
           <cfargument name="officeid">
             <cfquery  name="officequery" datasource="#mydatasource#"> 
                   Select officeid from office_tbl where officeid = '#officeid#' 
             </cfquery> 
                
                <cfset theStruct = StructNew()>   
                <cfset theStruct.officeid=#officeQuery.officeid#>
            <cfreturn   SerializeJSON(theStruct)> 
      </cffunction>
    I don't know much about ColdFusion, so no idea if this'll work.

    You'll need to keep the dataType: "json" in your $.ajax() call:

    Code:
    $.ajax({
        type: 'post',
        data: {officeid:id},
        dataType: 'json',
        url: 'toOtherpage.cfm',
        success:function(data){
          console.log(typeof data);
        }
    });

  14. #14
    SitePoint Wizard
    Join Date
    May 2012
    Posts
    1,157
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    HI pullo,

    I fixed it now...there is an element including in the response...i did not notice it.


    Thank you.

  15. #15
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    6,051
    Mentioned
    219 Post(s)
    Tagged
    12 Thread(s)
    That's good.
    Just for my own curiosity, what did you change?


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •