I have a web service in asp.net that register new members which works fine
using System;
using System.Collections.Generic;
using System. Linq;
using System.Web;
using System.Web.Services;
using System.Data;
using System.Data.SqlClient;
using System.Web.Script.Services;
namespace MyInfotechnology
{
/// <summary>
/// Summary description for CordovaWebService
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET
// AJAX, uncomment the following line.
[System.Web.Script.Services.ScriptService]
public class CordovaWebService : System.Web.Services.WebService
{
public string connectionstring = "Data Source="#".19;Initial
Catalog=DB_USERS;User ID="#";Password="#"";
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public void Insert(string uname, string pw, string frstname, string lstname)
{
using (SqlConnection con = new SqlConnection(connectionstring))
{
using (SqlCommand cmd = new SqlCommand("INSERT INTO users (username,
password, firstname, Lastname) VALUES (@username, @password,
@firstname, @Lastname)"))
{
cmd.Parameters.AddWithValue("@username", uname);
cmd.Parameters.AddWithValue("@password", pw);
cmd.Parameters.AddWithValue("@firstname", frstname);
cmd.Parameters.AddWithValue("@Lastname", lstname);
cmd.Connection = con;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
}
Here is my ajax call to post data
$(function(){
$('#btnLogin').click(function () {
var username = $('#Username').val();
var password = $('#Password').val();
var firstname = $('#frstname').val();
var lastname = $('#lstname').val();
if (username != '' && password != '' && firstname != '' && lastname
!= '') {
$.ajax
({
type: 'POST',
url: 'http://www.myinfotechnology.com/CordovaWebservice.asmx?
WSDL',
asyn: false,
data: {'username': '" + username + "', 'password': '" +
password + "', 'firstname':'" + firstname + "',
'lastname': '" + lastname + "'},
contentType: 'application/json; charset =utf-8',
success: function (data)
{
var obj = data.d;
if (obj == 'true') {
$('#Username').val('');
$('#Password').val('');
$('#frstname').val('');
$('#lstname').val('');
alert("Data Saved Successfully");
}
},
error: function (result) {
alert("Error Occured, Try Again");
}
});
} else
{
alert("Please Fill all the Fields");
return false;
}
})
});
</script>
The problem is that the ajax post function does not seems to work as what is supposed to do. Below is the error from ajax after calling the function. My main object is to have cordova phone application using asp.net environment to register new members and and login after registering. Any one help will be appreciated
Aucun commentaire:
Enregistrer un commentaire