开发者

JQuery $.ajax Request works in IE but not FF and Chrome

The following JQuery Request works fine in IE but not in FF and Chrome.

I am running the following page from its file location e.g. file:///C:/Test/json.htm and requesting a page running on localhost.

What is the reason for this?

And how can I get it to work for FF and Chrome?

<body>
<input type="button" value="Search" id="search-button" />
<script language="javascript" type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>

<script type="text/javascript">

$(function() {

    $('#search-button').click(function() {

    var parms = {
            id: 27  
        };

        $.ajax({
            type: 'POST',
            url: 'http://localhost:51621/Test/GetJSONMessage/',
            async: false,
            data: parms,
            dataType: 'json',
            success: function(data, testStatus) {
                alert(data.message);
        }
    });

   });

});
</script>
</body>

Where GetJSONMessage is supplied by an ASP.Net MVC JSonResult :

[HttpPost]
public JsonResult GetJSONMessage(int id)
{
    return Json(new { message = ("hello world " + id开发者_如何学C.ToString()) });
}


Because you're hosting from the filesystem, and making a request to localhost, Chrome and FF will see this as a cross domain request, and therefore a security issue.

If you type the URL directly into the location bar, you'll probably get your response.

Safari is a little more easy going about those "security issues" when you're hosting from the filesystem.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜