PDA

View Full Version : [CLOSED] for submit to webservice



odyssey
Jul 01, 2013, 8:37 AM
I am in trouble to make correct call to web service. Form values are being submitted correctly the only problem is that webservice respond in xml despite that [System.Web.Script.Services.ScriptService] is uncomment, It throws an error on response. Ext.JSON.decode(): You're trying to decode an invalid JSON String:
for exampe


createNewAssets: function () {
var frm = Ext.getCmp('frmAddNewAsset').getForm();
frm.submit({
url: '../../stores/assets.asmx/saveNewAssets',
cleanRequest: true,
json: true, //it is not working
params: {
newAsset: frm.getValues(true)
}
success: function (form, action) {
Ext.Msg.alert('Success', action.result.msg);
}
})

[WebMethod]
public string saveNewAssets() {
return "{'success' : true , 'msg' : 'Success' }" ;
}

Daniil
Jul 01, 2013, 2:07 PM
Hi @odyssey,

This option

json: true
deals with DirectEvents and DirectMethods, it doesn't deal with form submit.

You can use the jsonSubmit method.
http://docs.sencha.com/extjs/4.2.1/#!/api/Ext.form.Basic-cfg-jsonSubmit

Example

form.submit({
url: 'some URL',
jsonSubmit: true
});

Also I think you can remove these settings:

cleanRequest: true,
params: {
newAsset: frm.getValues(true)
}

Though, there is another problem. A JSON WebService response always contains "d" as a root. A form can't interpret it and will always fire a failure handler. Well, you can listen to a failure, then analyze a response. Though, personally, I would prefer to use an HTTP handler instead (ashx).

Also you can use an XML WebService. Here is an example of possible response.
http://docs.sencha.com/extjs/4.2.1/#!/api/Ext.form.action.Submit

Daniil
Jul 01, 2013, 2:44 PM
There is another approach - using a FormPanel's ErrorReader.

Page

<%@ Page Language="C#" %>

<%@ Register Assembly="Ext.Net" Namespace="Ext.Net" TagPrefix="ext" %>

<!DOCTYPE html>
<html>
<head runat="server">
<title>Ext.NET v2 Example</title>

<script>
var submit = function () {
App.FormPanel1.submit({
url: 'WebService1.asmx/Submit',
jsonSubmit: true,
success: function () {
console.log("success");
},
failure: function () {
console.log("failure");
}
});
};
</script>
</head>
<body>
<form runat="server">
<ext:ResourceManager runat="server" />

<ext:Model runat="server" Name="ErrorModel">
<Fields>
<ext:ModelField Name="id" />
<ext:ModelField Name="msg" />
</Fields>
</ext:Model>

<ext:FormPanel ID="FormPanel1" runat="server">
<ErrorReader>
<ext:JsonReader
Root="d"
ModelName="ErrorModel"
SuccessProperty="d.success" />
</ErrorReader>
<Items>
<ext:TextField runat="server" Text="Hello!" />
</Items>
</ext:FormPanel>

<ext:Button runat="server" Text="Submit" Handler="submit" />
</form>
</body>
</html>


WebService

using System.Web.Services;

namespace Work2
{
/// <summary>
/// Summary description for WebService1
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
[System.Web.Script.Services.ScriptService]
public class WebService1 : System.Web.Services.WebService
{
[WebMethod]
public object Submit()
{
return new
{
success = true
};
}
}
}

odyssey
Jul 02, 2013, 7:52 AM
Quite amazingly I could find submitted params in case where form is configured submitting json. Context.Request["Param1"] nor Context.Request.QueryString["Param1"] has any data but fiddler clearly shows that json being submitted. Strange

Daniil
Jul 02, 2013, 1:39 PM
There are two approaches for a JSON WebService to get parameters.

1. Changing a method signature to:

[WebMethod]
public object Submit(string Param1, string Param2, ...)


2. Reading parameters from Request.InputStream. Here is an example, search for the "Retrieve JSON data from Request.InputStream" header.
http://www.codeproject.com/Articles/43317/Saving-ASP-NET-Form-Data-with-jQuery-AJAX-and-JSON