Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

$.Ajax success data.value parse

Posted on 2014-02-09
4
Medium Priority
?
527 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 82

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

Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

Question has a verified solution.

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

I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
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)

705 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