Solved

$.Ajax success data.value parse

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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…

739 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