错误的Ajax调用结果,而不是更迭 [英] ajax call results in error instead of succes

查看:224
本文介绍了错误的Ajax调用结果,而不是更迭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的ASP.net MVC3项目,我使用了Ajax调用JSON数据发送到控制器中的一个公司创造actionmethod。但是,当我调试Ajax调用,它总是在错误的结果,而不是更迭的结果告终。

Ajax调用:

  $。阿贾克斯({
            网址:'/公司/创建,
            输入:POST,
            数据:JSON.stringify(CreateCompany)
            数据类型:Json的,
            的contentType:应用/ JSON的;字符集= UTF-8,
            成功:函数(){
                警报('ajax调用成功);
            },
            错误:函数(){
                警报('ajax调用不成功');
            }
        });

本公司控制我的操作方法:

  [HttpPost]
    公众的ActionResult创建(公司公司)
    {
        尝试
        {
            //创建公司
            CompanyRepo.Create(公司);
            返回null;
        }
        抓住
        {
            返回视图(错误);
        }
    }

我已经调试的actionmethod,但他像他应该完成它。
因此,该数据与Ajax调用发送将被处理并写入到数据库。 (操作方法不使用扣部分)。

为什么我的Ajax调用仍然给出消息Ajax调用没有成功的?


解决方案

我用了同样的问题又回到了JSON结果


我所做的是设置数据类型为文本JSON:))
如果这没有帮助尝试通过收购你的错误的详细信息,即以获得更多信息。

  $。阿贾克斯({
        网址:'/公司/创建,
        输入:POST,
        数据:JSON.stringify(CreateCompany)
        数据类型:文字JSON,
        的contentType:应用/ JSON的;字符集= UTF-8,
        成功:函数(){
            警报('ajax调用成功);
        },
        错误:功能(XMLHtt prequest,textStatus,errorThrown){
            警报(XMLHtt prequest =+ XMLHtt prequest.responseText +\\ ntextStatus =+ textStatus +\\ nerrorThrown =+ errorThrown);
        }
    });

BTW:我发现这个解决方案在计算器的地方

In my ASP.net mvc3 project, i use a ajax call to send json data to a create actionmethod in the controller Company. But when i debug the ajax call, it always end up in a error result instead of the succes result.

ajax call:

$.ajax({
            url: '/Company/Create',
            type: 'POST',
            data: JSON.stringify(CreateCompany),
            dataType: 'Json',
            contentType: 'application/json; charset=utf-8',
            success: function () {
                alert('ajax call successful');
            },
            error: function () {
                alert('ajax call not successful');
            }
        });

My action method in the Company controller :

    [HttpPost]
    public ActionResult Create (Company company)
    {
        try
        {
            //Create company
            CompanyRepo.Create(company);
            return null;
        }
        catch
        {
            return View("Error");
        }
    }

I already debugged the actionmethod, but he completes it like he should. So the data send with the ajax call will be handled and written to the db. (the action method does not use the catch part).

Why is my ajax call still gives the message 'ajax call not succesful'?

解决方案

I used to got same problem with getting back the JSON result. What I did is to set the dataType to "text json" :)) If this doesn't help try to get additional info by acquiring details of your error, i.e.:

$.ajax({
        url: '/Company/Create',
        type: 'POST',
        data: JSON.stringify(CreateCompany),
        dataType: 'text json',
        contentType: 'application/json; charset=utf-8',
        success: function () {
            alert('ajax call successful');
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            alert("XMLHttpRequest=" + XMLHttpRequest.responseText + "\ntextStatus=" + textStatus + "\nerrorThrown=" + errorThrown);
        }
    });

BTW: I found this solution somewhere on the StackOverflow

这篇关于错误的Ajax调用结果,而不是更迭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆