0

I'm using ajax call but I don't get any answer. In the next image you can see that the call works correctly but i don't get a data. enter image description here

My html is the next code:

<script> 
        // wait for the DOM to be loaded 
        $(document).ready(function() { 

            // bind 'myForm' and provide a simple callback function

            $('#form input').on('change', function() {  
                var valueSelected = $("#form").find('input:checked').val();         


                /*$.post("http://localhost:49918/Home/HandleForm", {howGood : valueSelected}, function(respuesta) {
                    console.log("La respuesta es:", respuesta)
});*/

                 $.ajax({
                       type: "POST",

                       url:   "http://localhost:49918/Home/HandleForm",

                       data: { howGood : valueSelected },
                     success: function(respuesta){
                     console.log("La respuesta es:", respuesta)
                     },
                     error: function(respuesta){
                     console.log("Fail:", respuesta)
                     }
                 });


            }); 
        }); 
    </script> 

<div class="content-area">
    <h1>How Good Are You</h1>
        <form name="form" id="form" method="post">
            <input type="radio" name="howGood" value="Excellent">Excellent<br>
            <input type="radio" name="howGood" value="VeryGood">Very Good<br>
            <input type="radio" name="howGood" value="Good">Good<br>
            <input type="radio" name="howGood" value="Average">Average<br>
            <input type="radio" name="howGood" value="Poor">Poor
            <br>
</form>             
    </div>

and my code in the server is(It works perfctly, I can debbug successly):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Web.Services;

namespace MvcApplication3.Controllers
{
    [HandleError]
    public class HomeController : Controller
    {
        public ActionResult Index()
        {

            return View();
        }

         [WebMethod]
         public string HandleForm()
        {
            string howGood = null; 
            try
            {
                howGood = Request.Form["howGood"];
                string connectionString = ConfigurationManager.ConnectionStrings["indivirtualTest"].ConnectionString;

                SqlDataAdapter SqlDataAdapter = new SqlDataAdapter();
                SqlCommand SqlCommand = new SqlCommand();

                SqlConnection conection = new SqlConnection(connectionString);
                conection.Open();
                string query = "UPDATE Howgoodareyou SET " + howGood + " = " + howGood + " + 1";
                SqlCommand myCom = new SqlCommand(query, conection);
                myCom.ExecuteNonQuery();
               // SqlDataReader dr = SqlCommand.ExecuteReader(CommandBehavior.CloseConnection);
                conection.Close();
            }
            catch (Exception e) { throw e;  }

            return howGood;
        }
    }
}

Can someone help me? I don't get any answer.... but the code is execute correctly.

Thank you,

Aroma
  • 173
  • 7
  • what you get from response from e.g. firebug? http status, response, etc.? – shpyo Dec 30 '13 at 10:59
  • Is the ajax call is cross domain or on same domain? – Jai Dec 30 '13 at 11:01
  • statusText --> "error"; responseTest --> "" (You can see it in the first picture) – Aroma Dec 30 '13 at 11:15
  • It's in the same domain. And It's working correctly. – Aroma Dec 30 '13 at 11:22
  • can you change 'error: function(respuesta){ console.log("Fail:", respuesta) }' to 'error: function(respuesta){ console.log(respuesta); }' and put the result here? – Mateusz Dec 30 '13 at 11:42
  • Hi, My page is in: http://localhost:16368 and the server code is executing in another port. Is this the problem? – Aroma Dec 30 '13 at 17:54

2 Answers2

1

You can't reference external URLs with ajax. The url parameter must be local.

You can, however, have a local server script grab the data, and then perform an ajax request on that script.

kuroi neko
  • 8,479
  • 1
  • 19
  • 43
  • Hi, My page is in: http://localhost:16368 and the server code is executing in another port. What can I do? – Aroma Dec 30 '13 at 17:53
  • I'm not familiar at all with ASP.net, so I could hardly give you knowledgeable advices here. I think [this topic](http://stackoverflow.com/questions/2099728/how-do-i-send-an-ajax-request-on-a-different-port-with-jquery) could be of some help, but for a puny PHP user like me it seems awfully complicated :) – kuroi neko Dec 31 '13 at 00:35
0

Hi Aroma Please use the below code for call ajax. I have changed javascript function.

<div class="content-area">
<h1>How Good Are You</h1>
    <form name="formnew" id="formnew" method="post">
        <input type="radio" name="howGood" value="Excellent" onchange="callUrl(this);">Excellent<br>
        <input type="radio" name="howGood" value="VeryGood" onchange="callUrl(this);">Very Good<br>
        <input type="radio" name="howGood" value="Good" onchange="callUrl(this);">Good<br>
        <input type="radio" name="howGood" value="Average" onchange="callUrl(this);">Average<br>
        <input type="radio" name="howGood" value="Poor" onchange="callUrl(this);">Poor
        <br>


function callUrl(obj) {
    var valueSelected = $(obj).val();
    $.ajax({
        type: "POST",

        url: "http://localhost:49918/Home/HandleForm",

        data: { howGood: valueSelected },
        success: function (respuesta) {
            console.log("La respuesta es:", respuesta)
        },
        error: function (respuesta) {
            console.log("Fail:", respuesta)
        }
    });
}
</script> 
Mayur Tilva
  • 160
  • 8
  • Hi Aroma Please add the debug point on HandleForm() method and check I think you got the error from server code.(from HandleForm() method) – Mayur Tilva Dec 30 '13 at 11:20
  • Thank you, The code is correct, I've debbugged a lot of times and It works... :( actually, inserts to the bbdd correctly. But after that, the ajax don't get the response. – Aroma Dec 30 '13 at 11:29
  • Add the $ajax function outside the documenet.ready() function and try it. – Mayur Tilva Dec 30 '13 at 11:36
  • Hi I have updated my answer and changed the code and now it will be working. – Mayur Tilva Dec 31 '13 at 05:22