简体   繁体   中英

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM