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!