使用Jquery Ajax将对象作为数据发布 [英] Post an object as data using Jquery Ajax

查看:72
本文介绍了使用Jquery Ajax将对象作为数据发布的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试过的代码如下:

var dataO = new Object();
dataO.numberId = 1;
dataO.companyId = 531;

$.ajax({
 type: "POST",
 url: "TelephoneNumbers.aspx/DeleteNumber",
 data: "{numberId:1,companyId:531}",
 contentType: "application/json; charset=utf-8",
 dataType: "json",
 success: function(msg) {
 alert('In Ajax');
 }
});

我想将对象dataO作为ajax数据传递,怎么办?

I would like to pass the object dataO as the ajax data, how can I do it?

推荐答案

我将保留原始答案,但以下是您解决该问题的方法. (原谅我,但自从我将常规asp.net/web服务与jquery一起使用以来已经很长时间了)

I will leave my original answer in place but the below is how you need to approach it. (Forgive me but it is a long time since I have used regular asp.net / web services with jquery:)

您需要使用以下js库 json2 库,然后可以使用stringify方法来确保您的json格式符合该服务的正确格式.

You need to use the following js lib json2 library, you can then use the stringify method to ensure your json is in the correct format for the service.

var data0 = {numberId: "1", companyId : "531"};

var json = JSON2.stringify(data0 ); 

$.ajax({
 type: "POST",
 url: "TelephoneNumbers.aspx/DeleteNumber",
 data: json,
 contentType: "application/json; charset=utf-8",
 dataType: "json",
 success: function(msg) {
 alert('In Ajax');
 }
});

更新:同一问题/回答此处

UPDATE: Same issue / answer here

这篇关于使用Jquery Ajax将对象作为数据发布的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆