ajax在项目中怎么使用,我如何添加项目在sql中使用jQuery(ajax)通过web服务

我有一个web服务,并在其中有两种方法(select,insertdata)。我想用jquery在sql中插入一条记录。我怎样才能做到这一点?我已经制作了该代码,但它不起作用。请帮助我。我如何添加项目在sql中使用jQuery(ajax)通过web服务

我的web服务

Sqlconeection dd = new Sqlconeection();

int rowsInserted = 0;

[WebMethod]

[ScriptMethod(ResponseFormat = ResponseFormat.Json)]

public string insertRecord(Int64 mbn,string name,double amt,bool Notify)

{

SqlConnection connection = dd.getconnection();

SqlCommand cmd = new SqlCommand("InsertData");

cmd.Connection = connection;

if (connection.State == ConnectionState.Closed)

connection.Open();

// string inser = "insert into expensive(mobileNumber,Name,Amount,Notify)values('" + mbn + "','" + name + "','" + amt + "','" + Notify + "')";

//cmd.CommandText = inser;

cmd.Parameters.AddWithValue("@mobile", mbn);

cmd.Parameters.AddWithValue("@Name", name);

cmd.Parameters.AddWithValue("@Amount", amt);

cmd.Parameters.AddWithValue("@Notify", Notify);

cmd.CommandType = CommandType.StoredProcedure;

rowsInserted= cmd.ExecuteNonQuery();

return string.Format("Thank you, {0} number of rows inserted!", rowsInserted);

}

[WebMethod(Description = "Returns all Products")]

[ScriptMethod(ResponseFormat = ResponseFormat.Json)]

public DataTable selectdata(Int64 mnb)

{

DataTable dt = new DataTable();

SqlConnection conn = dd.getconnection();

SqlCommand cmd = new SqlCommand();

cmd.Connection = conn;

if (conn.State == ConnectionState.Closed)

conn.Open();

string select = "select mobileNumber,Name,Amount,Notify from expensive where mobileNumber='" + mnb + "'";

cmd.CommandText = select;

SqlDataReader dr = cmd.ExecuteReader();

dt.TableName = "expensive";

dt.Load(dr);

return dt;

}

我API代码

$(function() {

$('#btnSubmit').click(function() {

var mob = $('#txtmo').val();

var Nm = $('#txtName').val();

var amout = $('#txtAmt').val();

var notify = $('#txtnoty').val();

$.ajax({

type: "POST",

url: "InsertData.asmx/insertRecord",

data: "{ mob: '" + mob + "', Nm: '" + Nm + "',amount:'" + amout + "',notify:'" + notify + "'}",

contentType: "application/json; charset=utf-8",

dataType: "jsonp",

success: function (data) {

var obj = data.d;

if (obj == 'true') {

$('#txtmo').val('');

$('#txtName').val('');

$('#txtAmt').val('');

$('#txtnoty').val('');

$('#lblData').html(JSON.stringify(data.d));

}

},

error: function (r) {

console.log(r);

},

});

});

});

2015-10-15

Raj Gola

+0

请提供一些更多的细节,并显示为代码剪断,而不是整个程序。 –

+0

您的数据对象属性名称与您webmethod的启动项不匹配。 –

+0

同样在您的成功中,您检查webmethod是否返回'true',但它实际上会返回成功消息。 –

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个基于Mybatis和Ajax的JavaWeb项目实现,可以从数据库获取数据并将其显示在页面。 1. 首先,需要创建一个数据库表,例如“user”,包含id、name和age字段。 2. 接下来,需要创建一个JavaBean类User,用于将数据库表的数据映射到Java对象。 ```java public class User { private int id; private String name; private int age; // getter and setter } ``` 3. 然后,需要创建一个Mybatis的映射文件UserMapper.xml,用于定义SQL语句以及与JavaBean类的映射关系。 ```xml <mapper namespace="com.example.mapper.UserMapper"> <select id="getAllUsers" resultType="com.example.model.User"> SELECT id, name, age FROM user </select> </mapper> ``` 4. 接下来,需要创建一个UserDao接口,用于定义数据访问操作。 ```java public interface UserDao { List<User> getAllUsers(); } ``` 5. 然后,需要创建一个UserDaoImpl类,实现UserDao接口,并注入Mybatis的SqlSessionFactory对象。 ```java public class UserDaoImpl implements UserDao { private SqlSessionFactory sessionFactory; public void setSessionFactory(SqlSessionFactory sessionFactory) { this.sessionFactory = sessionFactory; } @Override public List<User> getAllUsers() { SqlSession session = sessionFactory.openSession(); try { UserMapper mapper = session.getMapper(UserMapper.class); return mapper.getAllUsers(); } finally { session.close(); } } } ``` 6. 接下来,需要创建一个Servlet类UserServlet,用于处理Ajax请求并调用UserDao获取数据。 ```java public class UserServlet extends HttpServlet { private UserDao userDao; public void setUserDao(UserDao userDao) { this.userDao = userDao; } @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { List<User> userList = userDao.getAllUsers(); Gson gson = new Gson(); String json = gson.toJson(userList); response.setContentType("application/json"); response.setCharacterEncoding("UTF-8"); response.getWriter().write(json); } } ``` 7. 最后,需要在页面使用Ajax调用Servlet并将获取到的数据显示在页面。 ```html <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Mybatis and Ajax Example</title> <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script> <script> $(document).ready(function() { $.ajax({ url : "user", type : "GET", dataType : "json", success : function(data) { var table = $("<table></table>").addClass("table"); var header = $("<tr></tr>").append( $("<th></th>").text("ID"), $("<th></th>").text("Name"), $("<th></th>").text("Age") ).appendTo(table); $.each(data, function(index, user) { $("<tr></tr>").append( $("<td></td>").text(user.id), $("<td></td>").text(user.name), $("<td></td>").text(user.age) ).appendTo(table); }); $("#userList").append(table); } }); }); </script> </head> <body> <h1>Mybatis and Ajax Example</h1> <div id="userList"></div> </body> </html> ``` 以上就是一个基于Mybatis和Ajax的JavaWeb项目实现,可以从数据库获取数据并将其显示在页面

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值