JQuery Ajax Post callback function does not perform the solution to the problem _jquery

Source: Internet
Author: User

Today, when you write a feature that checks the username, you use the Jquery.post (URL, [data], [callback], [type]) function, but find that the callback function is not executed.

Take a look at my Code first:

Foreground code:

<script type= "Text/javascript" >
function CheckUser () {
var user = $ (' #<%=txtuser.clientid%> '). Val ();
$.post (' Checkuser.ashx ', {Action: "Check", name:user},
function (data) {
alert (data);
},
"JSON"); c10/>}
</script>

Background code (CHECKUSER.ASHX):

public void ProcessRequest (HttpContext context)
{context
. Response.ContentType = "Application/json";
String action = context. request["Action"];
String user = context. request["Name"];
String result = "{result:0}";
Switch (action)
{case
' Check ':
if (us.isexists (user))
{result
= ' {result:1} ';
}
break;
Context. Response.Write (result);
}

But there is no way to execute the alert (data) statement in the callback function, after stepping through it, and finally finding that there is a problem with the returned JSON string.

The key to the JSON string returned in jquery is enclosed in quotes, such as {"Result": 1}.

Let's take a look at jquery Ajax. Reasons for not performing callback function success

jquery Ajax does not perform callback function success reason:

$.ajax ({
type: Post),
contentType: "Application/json",
url:basepath+ "pages/tongji/ Disposeagree.action?cepingitem= "+cepingitem+" &userrelation= "+userrelation,
//data: {fenshu:8},
DataType: "JSON",
success:function (result) {
alert ("Operation succeeded");
},
error:function (response) {
alert ("error");
}
);

As above, because the property datatype: "JSON" is set, the callback function success is never executed, and the error function is executed only if the background returns not the correct JSON string. Either return the correct JSON string or change the DataType property to text

The correct return of the JSON string is as follows:

Pw.write ("{\" name\ ": \" Zs\ "}");
Pw.write ("{\" name\ ":" +true+ "}");

Examples of errors:

Pw.write ("{' Name ': ' Zs '}");

The above is a small set to introduce the jquery Ajax Post callback function does not solve the problem, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.