Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>user1502890,</p> <h2>Server-side</h2> <p>I don't really know C# but it appears that you are trying to build a JSON string directly. It is far more normal (and easier and more reliable) to build an object and then to stringify it into JSON with either a built-in language command or a utility method. I guess this must be possible in C#.</p> <h2>Client-side</h2> <p>Assuming the credentials fields to be in a form with <code>id="credentialsForm"</code> and the server-side script to be requestable as the relative URL "myCredentialsChecker.xtn", then the jQuery will be something like this:</p> <pre><code>$(function() { function loginResponse(j) { j = j || {}; if (j.LoginSuccess) { //... } else { $('#txtUserName').attr("class", j.txtUserName ? j.txtUserName : "Input"); $('#txtPassword').attr("class", j.txtPassword ? j.txtPassword : "Input"); $('#txtTestTokenNumber1').attr("class", j.txtTestTokenNumber1 ? j.txtTestTokenNumber1 : "Error"); $('#txtTestTokenNumber2').attr("class", j.txtTestTokenNumber2 ? j.txtTestTokenNumber2 : "Error"); $('#txtTestTokenNumber3').attr("class", j.txtTestTokenNumber3 ? j.txtTestTokenNumber3 : "Error"); $('#txtTestTokenNumber4').attr("class", j.txtTestTokenNumber4 ? j.txtTestTokenNumber4 : "Error"); $('#ErrorControl').html(''); } } $("#credentialsForm").on('submit', function() { $.ajax({ url: "myCredentialsChecker.xxx", data: $(this).serialize(), type: 'POST', // or 'GET' dataType: 'json', success: function(j) { loginResponse(j); ), error: function() { loginResponse(); } }); return false; }); }); </code></pre> <p><strong>Notes</strong></p> <ul> <li><p>Everything in the <code>else{...}</code> clause simplifies as above because <code>''</code> and <code>undefined</code> are both falsy, so simply testing <code>foo</code> will do the job. The ternary alternaive to <code>if(){...} else{...}</code> also makes for compact code.</p></li> <li><p>I'm not convinced that setting classes to the returned strings is the right thing to do but I've not changed this aspect.</p></li> </ul>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload