Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

$.Ajax success data.value parse

Posted on 2014-02-09
4
Medium Priority
?
532 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 300 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 83

Accepted Solution

by:
leakim971 earned 1400 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 44

Assisted Solution

by:Chris Stanyon
Chris Stanyon earned 300 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction HyperText Transfer Protocol (http://www.ietf.org/rfc/rfc2616.txt) or "HTTP" is the underpinning of internet communication.  As a teacher of web development I have heard many questions, mostly from my younger students who have come to t…
The most up-to-date version of this article is on my Blog https://iconoun.com/blog/
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…
Suggested Courses

824 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