Solved

$.Ajax success data.value parse

Posted on 2014-02-09
4
506 Views
Last Modified: 2014-02-12
Hello,

I'm trying to return the value in the success (data) part of my $ajax call. I've looked through Google but what I'm looking for is a coded solution below. I don't think it's that complex from similar examples I've seen. I just can't seem to get mine working.

When I add alert(data); I get...

<wddxPacket version='1.0'><header/><data><string>1</string></data></wddxPacket>


but I would like to be able to run 2 different functions if <string> eq "1" or "". Is that possible?
 	$.ajax({
		type: "POST",
		url: "app/func_main.cfc?method=login",
               data: data,
		cache: false,
		success: function (data) {

			alert(data);

                      if (data.string == 1) {
                           function1();
                     } else {
                          function2();
                     }

 		},
 		error: function (xhr, ajaxOptions, thrownError) {
        alert(xhr.status);
        alert(thrownError);
      	}

	});

Open in new window

0
Comment
Question by:brihol44
4 Comments
 
LVL 35

Assisted Solution

by:Robert Schutt
Robert Schutt earned 75 total points
ID: 39846945
It looks like you need to add the data type to your ajax call, then 'data' in the callback will be an xml document and you can use the following code to determine which function to call:
    $.ajax({
        type: "POST",
        url: "app/func_main.cfc?method=login",
        data: data,
        dataType: 'xml',
        cache: false,
        success: function (data) {

            //alert(data);

            try {
                // use jQuery functionality to read xml to avoid browser specific code
                var str = $($.find('data > string', data)[0]).text();

                if (Number(str) === 1) {
                    function1();
                } else {
                    function2();
                }

            } catch (e) {
                alert('error while parsing xml: ' + e.message);
            }

        },
        error: function (xhr, ajaxOptions, thrownError) {
            alert(xhr.status);
            alert(thrownError);
        }

    });

Open in new window

0
 
LVL 82

Accepted Solution

by:
leakim971 earned 350 total points
ID: 39846977
check this : http://jsfiddle.net/vB6V9/
alert(data);

var str = $("string",data).text() * 1;
if (str == 1) {
    function1();
} else {
    function2();
}

Open in new window

0
 
LVL 42

Assisted Solution

by:Chris Stanyon
Chris Stanyon earned 75 total points
ID: 39847193
In your question you indicate that you want to check against a string value of 1 - "1", not a number. If that's the case then this in the success handler:

$string = $('string', data).text();
if ($string == '1') {
   function1();
} else {
   function2();
}

Open in new window

0
 

Author Closing Comment

by:brihol44
ID: 39855348
Thanks for the help everybody! I tried them all but  leakim971's solution was the only one I got to work. Maybe I missed something on the others but it's exactly what I needed. Thanks!
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
Introduction If you're like most people, you have occasionally made a typographical error when you're entering information into an online form.  And to your consternation, the browser remembers the error, and offers to autocomplete your future entr…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now