Jump to content

how to get first letter of a string in ajax response


shadd
Go to solution Solved by shadd,

Recommended Posts

i have this php script:

    $result='Osucess'
echo $result;

i have this jquery ajax:


 	$("#add_sub").submit(function (e) { 
		e.preventDefault();
		var formDatax = new FormData($('#add_sub')[0]); 
$.ajax({
				type: "POST",
				url: 'answers.php',
				data:  formDatax,

			    //Options to tell jQuery not to process data or worry about content-type.
                cache: false,
                contentType: false,
                processData: false,
			success: function(data){

				
				if(data.charAt(0) ==="O"){
					alert(data);
					
				}else{
					alert("failed");				}
				
			}	
				
		});
});

but it does not work at all.what is the problem??

Link to comment
Share on other sites

Posted (edited)
7 hours ago, Barand said:

I'd start by checking what data contains with

sole.log(data)

it contains "Osucess" but the if tatements do not work out

And when i try console.log(data[0]);

it shows nothing at all.

 

Edited by shadd
clarity
Link to comment
Share on other sites

  • Solution

this is what i did to get it

var str= data ;
                let removedSpacesText = str.split(" ").join("");
                alert(removedSpacesText);
                alert(removedSpacesText[0]);

and it worked

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.