mardi 21 juin 2016

jQuery MVC Controller return error

I have an ASP.NET MVC3 app and a button in the following view that is supposed to send the drop down values from two kendo drop down lists to a control method. I have writen a jQuery function to send the data as follows in the View file:

    $("#get").click(function () {
        alert('Thank you! You picked:\n\nAsset1: ' + dropdownVals.first_asset + '\n\n ' + 'and\n\n' + ' Second Asset: ' + dropdownVals.second_asset);
        $.ajax({
            type: "POST",
            url: "/Asset/RunPy", // the method we are calling
            contentType: "application/json",
            data: {as_1: dropdownVals.first_asset, as_2: dropdownVals.second_asset},
            dataType: "json",
            success: function (result) {
                alert('Yay! It worked!');
                // Or if you are returning something
                alert('I returned... ' + result.WhateverIsReturning);
            },
            error: function (result) {
                alert('Oh no :(');
            }
        });
    }); 

Going to the following Controller:

[HttpPost]
        public ActionResult RunPy(String as_1, String as_2)
        {
            String cmd = "C:/Program Files (x86)/Python27/python.exe";
            String argss = "C:/My_Python_lib/happyBirthday.py joe bill";
            ProcessStartInfo start = new ProcessStartInfo();
            start.FileName = cmd;
            start.Arguments = argss;
            start.UseShellExecute = false;
            start.RedirectStandardOutput = true;
            using (Process process = Process.Start(start))
            {
                using (StreamReader reader = process.StandardOutput)
                {
                    string result = reader.ReadToEnd();
                    Response.Write(result);
                    List<string> resultList = new List<string>();
                    resultList.Add(result);


                    return Json(new { success = true });

                }


            }



        }
    }

I don't know where the issue is arising but, the error event in the jQuery function is firing up.

However, if I call the same Controller method using another button class as follows, then I get no errors.

<form class="float_left" action="RunPy" method="post">
    <button class="btn btn-inverse" title="Connect" type="submit">Make Connection</button>
</form>

Aucun commentaire:

Enregistrer un commentaire