Solved

$.Ajax success data.value parse

Posted on 2014-02-09
4
508 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 43

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

In this article you'll learn how to use Ajax calls within your CodeIgniter application. To explain this, I'll illustrate how to implement a simple contact form to allow visitors to send you an email through your web site.
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to dynamically set the form action using jQuery.
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)

920 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

13 Experts available now in Live!

Get 1:1 Help Now