Prayer is conditional upon being consistent with God’s will.
Matthew 21:21 (RSV) And Jesus answered them, “Truly, I say to you, if you have faith and never doubt, . . . even if you say to this mountain, ‘Be taken up and cast into the sea,’ it will be done.”
Mark 11:24 Therefore I tell you, whatever you ask in prayer, believe that you have received it, and it will be yours.
John 16:23-24 . . . Truly, truly, I say to you, if you ask anything of the Father, he will give it to you in my name.
 Hitherto you have asked nothing in my name; ask, and you will receive, that your joy may be full.
Passages like these have led atheist anti-Christian polemicists to claim that all prayer (supposedly according to the Bible) is unconditional. And indeed, even among Christians, there is a false strain of belief called the “prosperity gospel” or “name-it-claim-it” whereby the believer can have whatever he asks for, if only enough faith is exercised.
Both parties are exhibiting a rather naive and simplistic view. Biblical doctrine (regarding prayer or anything else) has to be determined by considering all the relevant passages together.
Prayer is conditional upon being consistent with God’s will. So if we pray (to use an extreme example) for a difficult neighbor to be struck down and not able to talk or walk, that wouldn’t be in God’s will and God wouldn’t answer it.
Even something not immediately immoral or amoral wouldn’t necessarily be in God’s will, because He knows everything and can see where things might lead; thus may refuse some requests. When Jesus says “ask and you shall receive,” etc., it’s in a familiar Hebrew proverbial sense, which means that it is “generally true, but admits of exceptions.”
The Bible clear lays out several conditions for answered prayer:
1 John 5:14 And this is the confidence which we have in him, that if we ask anything according to his will he hears us.
James 4:3 You ask and do not receive, because you ask wrongly, to spend it on your passions.
James 1:7-8 For that person must not suppose that a double-minded man, unstable in all his ways, will receive anything from the Lord.
James 5:16 . . . The prayer of a righteous man has great power in its effects.
Hebrews 10:22 let us draw near with a true heart in full assurance of faith, with our hearts sprinkled clean from an evil conscience and our bodies washed with pure water.
Psalm 66:18 If I had cherished iniquity in my heart, the Lord would not have listened.
Proverbs 15:8 The sacrifice of the wicked is an abomination to the LORD, but the prayer of the upright is his delight.
Isaiah 1:15 When you spread forth your hands, I will hide my eyes from you; even though you make many prayers, I will not listen; your hands are full of blood. (cf. Jer 11:11)
Isaiah 59:2 but your iniquities have made a separation between you and your God, and your sins have hid his face from you so that he does not hear.
Even St. Paul’s petitionary prayer request was expressly turned down by God:
2 Corinthians 12:7-9 And to keep me from being too elated by the abundance of revelations, a thorn was given me in the flesh, a messenger of Satan, to harass me, to keep me from being too elated.  Three times I besought the Lord about this, that it should leave me;  but he said to me, “My grace is sufficient for you, for my power is made perfect in weakness.” I will all the more gladly boast of my weaknesses, that the power of Christ may rest upon me.
Many Bible scholars are of the opinion that Paul’s “thorn” was an eye disease of some sort.
The prophet Jonah prayed to God to die (Jonah 4:3): “Therefore now, O LORD, take my life from me, I beseech thee, for it is better for me to die than to live.” (cf. 4:8-9). God obviously didn’t fulfill the request, and chided Jonah or his anger (4:4, 9). The prophet Ezekiel did the same: “O LORD, take away my life” (1 Kgs 19:4). God had other plans, as the entire passage shows. If we pray something stupidly, God won’t answer. He knows better than we do.
Jesus also tells the story (not a parable, which don’t have proper names) in Luke 16 of Lazarus and the rich man, in which two petitionary requests (in effect, prayers: 16:24, 27-28, 30) to Abraham are turned down (16:25-26, 29, 31). Since Jesus is teaching theological principles or truths, by means of the story, then it follows that it’s His own opinion as well: that prayers are not always answered. They have to be according to God’s will.
The same principle is also applied to worship, which God will not receive if it is not offered with sincerity and absent serious sin:
Amos 5:14, 21-24 Seek good, and not evil, that you may live; and so the LORD, the God of hosts, will be with you, as you have said . . .  I hate, I despise your feasts, and I take no delight in your solemn assemblies.  Even though you offer me your burnt offerings and cereal offerings, I will not accept them, and the peace offerings of your fatted beasts I will not look upon.  Take away from me the noise of your songs; to the melody of your harps I will not listen.  But let justice roll down like waters, and righteousness like an ever-flowing stream.
Proverbs 21:27 The sacrifice of the wicked is an abomination; how much more when he brings it with evil intent. (cf. Jer 6:19-20; Mal 1:6-14; Jas 1:26-27)
When His people obeyed His commands, however, then God was pleased with the same sacrifices (see, e.g., Is 56:6-7; Jer 17:24-26; Mal 1:11: “a pure offering”; many others).