This is the first time i am playing with Ajax. I am trying to just create a basic login at the moment. Anyways i have the following script..
<script language = "javascript">
function Login() {
$.ajax({
type: "POST",
url: "ajax.aspx/Login",
data: '{' +
'username:"' + $('#username').val() + '",' +
'password:"' + $('#password').val() + '"' +
'}',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg) {
alert("alert1");
var data = json_parse(msg.d);
alert("alert2");
if (!data.error) {
alert("No Error);
}
else {
alert(data.error);
}
},
error: function(msg) {
alert('Failure: ' + msg);
}
});
}
</script>
For some reason it does not run anything after var data = json_parse(msg.d);
It shows the first two alerts but noting after.
the ajax page has the following `
[WebMethod]
public static string Login(string username, string password)
{
return "{\"error\": \"No IDs\"}";
}
Whats inside the function
json_parse? That function could be the source of your error.A better method to parse JSON is like this.
Now reference the script at the page you are pulling data from
<script src="path/to/script/json2.js" type="text/javascript"></script>Now inside the function, parse your JSON like this.
var data = JSON.parse(msg.d);On an un-related side note, its better if you change your
errorlike this to catch errors effectively.Also, call
WebMethodlike this.