JQuery的Ajax函数在chrome中工作,但在Firefox中返回404 [英] JQuery's Ajax function works in chrome but returns 404 in firefox

查看:554
本文介绍了JQuery的Ajax函数在chrome中工作,但在Firefox中返回404的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用中央ajax函数来向服务器发送ajax Post请求。这是函数的代码:

I'm using a central ajax function to send ajax Post requests to a server. This is the code of the function:

function postJson(url, jsObj, whenSuccess, whenError){
        $.ajax({
            type: "post",
            headers: {
                "Accept": "application/json, text/javascript, */*; q=0.01",
                "Content-Type": "application/json, text/javascript, */*; q=0.01"
            },
            dataType: "json",
            url: url,
            data: JSON.stringify(jsObj),
            success: function(result){
                if(whenSuccess !== undefined){ whenSuccess(result); }
            },
            error: function(xhr){
                if(whenError !== undefined){ whenError(xhr.status); }
            }
        });
    }

当我尝试运行我的应用程序时,它在Chrome中正常工作,它抛出一个404.我的REST服务帮助器返回一个404,当接受或内容类型没有设置为JSON ...所以我认为firefox可能不添加头,但当我看看发送的请求:

When I try to run my application it works fine in chrome, but in firefox it throws a 404. My REST service helper returns a 404 when the accept or content type isn't set to JSON... so I thought that firefox might not add the headers but when I look at the sent request:

Request URL:
http://localhost:9081/api/1/localize/validation.json

Request Method:
POST

Status Code:
HTTP/1.1 404 Not Found

Request Headers
08:40:10.000

X-Requested-With:XMLHttpRequestUser-Agent......
Referer:http://localhost:9081/kportal/
Pragma:no-cacheHost:localhost:9081
Content-Type:application/json, text/javascript; charset=UTF-8, */*; q=0.01
Content-Length:2
Connection:keep-alive
Cache-Control:no-cache
Accept-Language:en-US,en;q=0.5
Accept-Encoding:gzip, deflate
Accept:application/json, text/javascript, */*; q=0.01

您可以看到设置了必要的标题。

You can see that the necessary headers are set. Still I'm getting a 404 in firefox but not in chrome.

任何想法?

推荐答案

尝试此操作,

function postJson(url, jsObj, whenSuccess, whenError){
    $.ajax({
        type: "post",
        contentType: "application/json; charset=utf-8",
        accepts: {
            xml: 'text/xml',
            text: 'text/plain'
        },
        dataType: "json",
        url: url,
        data: JSON.stringify(jsObj),
        success: function(result){
            if(whenSuccess !== undefined){ whenSuccess(result); }
        },
        error: function(xhr){
            if(whenError !== undefined){ whenError(xhr.status); }
        }
    });
}

请参阅 jQuery ajax接受attrib的点是什么?

阅读 http://api.jquery.com/jquery.ajax/

这篇关于JQuery的Ajax函数在chrome中工作,但在Firefox中返回404的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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