jacky brown
jacky brown

Reputation: 271

Jquery AJAX to asmx web method get error as response

OK, so this is what I do:

client:

function AjaxCall(url, method, data, OnSuccessFunction, OnErrorFunction) {
    var ajaxData = data;
    ajaxData = JSON.stringify(ajaxData);
    var ajaxURL = url + "/" + method;
    $.ajax({
        type: "POST",
        url: ajaxURL,
        data: ajaxData,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function () {
            OnSuccessFunction.apply(this, arguments);
        },
        error: function () {
            $('.ProgressBar').hide();
            OnErrorFunction.apply(this, arguments);
        }
    });
}

$(document).ready(function () {

   AjaxCall("http://localhost:34714/WebService1.asmx", "GetChart", { filename: 'excel-demo.xlsx', sheet: 'chart_param' }, GetTradingViewSuccess, GetTradingViewError)

});

var GetTradingViewSuccess = function (data) {

}

var GetTradingViewError = function (jqXHR, textStatus, errorThrown) {

}

asmx:

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OleDb;
using System.Linq;
using System.Web;
using System.Web.Script.Services;
using System.Web.Services;
using WebApplication2.Classes;

namespace WebApplication3
{
    /// <summary>
    /// Summary description for WebService1
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class WebService1 : System.Web.Services.WebService
    {
         [System.Web.Script.Services.ScriptMethod()]
        [WebMethod]
        public string add()
        {
            return "sd";
        }

        [WebMethod(EnableSession = true)]
         public Chart GetChart(string filename, string sheet)
        {
           Chart c = new Chart();
           //do something

            return c;
        }
    }
}

web config:

<?xml version="1.0"?>

<configuration>
    <system.web>
      <webServices>
        <protocols>
          <add name="HttpGet"/>
          <add name="HttpPost"/>
        </protocols>
      </webServices>

      <compilation debug="true" targetFramework="4.5.1" />
      <httpRuntime targetFramework="4.5.1" />
    </system.web>

</configuration>

and I get every time error message:

POST http://localhost:34714/WebService1.asmx/GetChart 500 (Internal Server Error)send @ jquery-1.7.2.min.js:4f.extend.ajax @ jquery-1.7.2.min.js:4AjaxCall @ Common.js:5(anonymous function) @ chart.html:15o @ jquery-1.7.2.min.js:2p.fireWith @ jquery-1.7.2.min.js:2e.extend.ready @ jquery-1.7.2.min.js:2c.addEventListener.B @ jquery-1.7.2.min.js:2 errorThrown "Internal Server Error"

I put break point in asmx file - GetChart webmethod, but never reached it. what am i doing wrong?

Upvotes: 0

Views: 1016

Answers (1)

zgood
zgood

Reputation: 12571

// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.

Uncomment the Line below that.

// [System.Web.Script.Services.ScriptService]

becomes

[System.Web.Script.Services.ScriptService]

Upvotes: 2

Related Questions