【问题标题】:ajax call not sending dataajax调用不发送数据
【发布时间】:2014-01-17 06:00:37
【问题描述】:

我正在使用 ajax 调用,但我没有得到任何答案。在下一张图片中,您可以看到呼叫正常工作,但我没有得到数据。

我的html是下一个代码:

<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>

我在服务器中的代码是(它运行良好,我可以成功调试):

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;
        }
    }
}

有人可以帮助我吗?我没有得到任何答案....但是代码执行正确。

谢谢你,

【问题讨论】:

  • 您从例如的响应中得到什么萤火虫? http状态、响应等?
  • ajax调用是跨域还是同域?
  • statusText --> "错误"; responseTest --> ""(第一张图可以看到)
  • 它在同一个域中。它工作正常。
  • 你能把 'error: function(respuesta){ console.log("Fail:", respuesta) }' 改成 'error: function(respuesta){ console.log(respuesta); }' 并将结果放在这里?

标签: javascript jquery ajax


【解决方案1】:

您不能使用 ajax 引用外部 URL。 url 参数必须是本地的。

但是,您可以让本地服务器脚本获取数据,然后对该脚本执行 ajax 请求。

【讨论】:

  • 嗨,我的页面位于:localhost:16368,并且服务器代码正在另一个端口中执行。我能做什么?
  • 我对 ASP.net 一点也不熟悉,所以在这里我很难给你有见地的建议。我认为this topic 可能会有所帮助,但对于像我这样的弱 PHP 用户来说,这似乎非常复杂 :)
【解决方案2】:

Hi Aroma 请使用以下代码调用 ajax。我改变了javascript函数。

<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> 

【讨论】:

  • Hi Aroma 请在 HandleForm() 方法上添加调试点并检查我认为您从服务器代码中得到了错误。(来自 HandleForm() 方法)
  • 谢谢,代码是正确的,我已经调试了很多次,它工作正常... :( 实际上,正确插入到 bbdd。但在那之后,ajax 没有得到响应。
  • 在documenet.ready()函数外添加$ajax函数试试看。
  • 嗨,我已经更新了我的答案并更改了代码,现在它可以工作了。
猜你喜欢
  • 1970-01-01
  • 2023-03-10
  • 1970-01-01
  • 2014-05-28
  • 2023-02-02
  • 2020-09-19
  • 2017-01-12
  • 2018-04-13
  • 2011-02-21
相关资源
最近更新 更多