-1

I want to pass some value to server and it has to return one string.

Jquery version

 <script src="js/jquery-3.1.1.js"></script>

Here is my code:

 $('#btnSaveFile').click(function () {
    var fileName = $('#txtFileName').val();
    alert(fileName);
    $.ajax({
        url: 'ReportTotalSalesPivot.aspx/getFileExistOrNot',
        method: 'GET',      //method or type ?
        contentType: 'application/json',
        data: '{fileName:' + fileName +'}',  //UPDATED Line
        dataType: 'json',
        success: function (data) {
            alert('success');
            alert(data.d.exist);
        },
        error: function (error) {
            alert('fail');
            alert(error);
        }
    });
});

Aspx code

    [WebMethod]
    public static string getFileExistOrNot(string fileName)
    {
        string cs = ConfigurationManager.ConnectionStrings["HQWebMatajer13"].ConnectionString;
        using (SqlConnection con = new SqlConnection(cs))
        {
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = con;
            cmd.CommandText = "select ReportData FROM [HQWebMatajer].[dbo].[ReportSave] where Userfilename=@UserFileName  and ReportName=@ReportName";
            cmd.Parameters.AddWithValue("@UserFileName", fileName);                
            cmd.Parameters.AddWithValue("@ReportName", "TotalSales");
            con.Open();                
            var data = cmd.ExecuteScalar();
            if (data != null)
            {
                string exist = "dataExist";
                return exist;
            }
            else
            {
                string exist = "notExist";
                return exist;
            }
        }            
    }

Error Msg GET http://localhost:55047/ReportTotalSalesPivot.aspx/getFileExistOrNot?fileName:www} 500 (Internal Server Error)

ExceptionType:"System.InvalidOperationException"

Message:"An attempt was made to call the method 'getFileExistOrNot' using a GET request, which is not allowed."

StackTrace:" at System.Web.Script.Services.RestHandler.GetRawParams(WebServiceMethodData methodData, HttpContext context) ↵ at System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context, WebServiceMethodData methodData)".

I think this error it occurs in server side. But I don't know what is that

Updated

Error Message:"Invalid web service call, missing value for parameter:'fileName'."

Community
  • 1
  • 1
Liam neesan
  • 2,282
  • 6
  • 33
  • 72

2 Answers2

2

Send your data like below:

In object format

data: { fileName:fileName },

OR

As a String

data = "fileName="+filename;
Hikmat Sijapati
  • 6,869
  • 1
  • 9
  • 19
0

After one day I found What was my mistake.

This is the answer

data:'{fileName:"'+fileName+'"}'
Liam neesan
  • 2,282
  • 6
  • 33
  • 72