Ajax jquery无法将对象发送到服务器端方法。 [英] Ajax jquery can't able to send object to server side method.

查看:85
本文介绍了Ajax jquery无法将对象发送到服务器端方法。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

。我试图调用服务器端方法,这是一个对象。

来自Ajax。它没用。如果我接受像[int,string]这样的变量,这工作正常。



我尝试过的事情:



我试过这段代码。





服务器端代码:



for example . i am trying to call server side method which is a object.
From Ajax. it is no working . if i take variables like [int,string] this is working fine .

What I have tried:

I tried that code .


Server side Code :

public async Task<serviceresponse> AddPetrolStationAdmin(PetrolStationAdminDto perStationAdminDto)
{
    try
    {
        using (var c = CTemplateWcfClient.BuildContainer("PetrolStation"))
        {
            var service = c.Resolve<ipetrolstationservicefacade>();
            var response = await service.AddPetrolStationAdmin(perStationAdminDto);
            return response;
        }
    }
    catch (Exception ex)
    {
        var response = new ServiceResponse(false, ex.Message, "");
        return response;
    }
}







客户端脚本:< br $> b $ b




Client Side Script :

var sectionTypeVM9 = '{' +
       '"_address":{"_id":null,"stationName":"stationnam","_address":"address","_phoneNumber":"_phoneNumber","_isActive":"false","_createdDate":"createddate","_merchantNumber":"_merchantNumber","_isShopClosed":"_isShopClosed"},' +
       ' "_time":[{"_id":"","_dayId":"","_toTime":"","_isActive":"","_createdDate":"","_stationId":""},{"_id":"","_dayId":"","_toTime":"","_isActive":"","_createdDate":"","_stationId":""}],' +
       '"_typePrice":[{"_id":"","_type":"","_price":"","_isActive":"true","_createdDate":"","_stationId":""},{"_id":"","_type":"","_price":"","_isActive":"true","_createdDate":"","_stationId":""}]' +
       '}';


   alert(sectionTypeVM9);
   alert(JSON.stringify(sectionTypeVM9));

   var totalString = 'hi this is string';
   //alert(totalString);
   $.ajax({
       type: "POST",
       url: "FuelAdmin.aspx/GetFieldValuesToWeb",
       data: '{fieldsWithValues: "' + totalString + '" }',
       contentType: "application/json; charset=utf-8",
       dataType: "json",
       success: OnSuccess,
       failure: function (response) {
           alert(response.d);
       }
   });

推荐答案

.ajax({
type: POST
url: FuelAdmin.aspx / GetFieldValuesToWeb
data:' {fieldsWithValues:' + totalString + ' }'
contentType: application / json; charset = utf-8
dataType: json
成功:OnSuccess,
失败: function (回复){
alert(response.d);
}
});
.ajax({ type: "POST", url: "FuelAdmin.aspx/GetFieldValuesToWeb", data: '{fieldsWithValues: "' + totalString + '" }', contentType: "application/json; charset=utf-8", dataType: "json", success: OnSuccess, failure: function (response) { alert(response.d); } });


按照以下流程解决您的问题。



Javascript代码

Follow below process to resolve your problem.

Javascript code
<script src="jquery-1.8.2.min.js"></script>
<script type="text/javascript">        
    var sectionTypeVM9 = '{"fieldsWithValues":{"ID":"1","Name":"Manas"}}';


.ajax({
type: POST
url: < span class =code-string> AllTest.aspx / GetFieldValuesToWeb,
data:sectionTypeVM9,
contentType: < span class =code-string> application / json; charset = utf-8,
dataType: json
成功: function (响应){
alert(response.d);
},
失败:功能(响应){
alert(response.d);
}
} );

< / script>
.ajax({ type: "POST", url: "AllTest.aspx/GetFieldValuesToWeb", data: sectionTypeVM9, contentType: "application/json; charset=utf-8", dataType: "json", success: function (response) { alert(response.d); }, failure: function (response) { alert(response.d); } }); </script>



服务器方法


Server method

// This is a custom class
public class Temp
{
     public int ID { get; set; }
     public string Name { get; set; }
}

[WebMethod]
public static string GetFieldValuesToWeb(Temp fieldsWithValues)
{
	try
	{
		// Perform your logic
		return "Success";
	}
	catch (Exception ex)
	{
		 return ex.Message;
	}
}



注意:我使用Temp作为类名,在你的情况下你需要使用不同的名称。其次,当你以字符串格式传递json时,不需要使用Json.stringify()。


Note: I used Temp as class name, in your case you need to use different one. Secondly as you are passing json in string format then there is not need to use Json.stringify().


这篇关于Ajax jquery无法将对象发送到服务器端方法。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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