URL在浏览器中工作正常,但使用ajax请求给出404错误 [英] URL works Fine in browser but gives 404 error with ajax request

查看:808
本文介绍了URL在浏览器中工作正常,但使用ajax请求给出404错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我正在使用PHP脚本,并且遇到一个问题,当我从浏览器打开URL时,它会成功打开,但是当尝试发出ajax请求时,它会给我404错误.我的代码正在关注

Hello i am working on a PHP script and i am facing an issue that when i open the URL from browser it opens successfully but when try to make ajax request it gives me 404 Error. My Code is Following

jQuery.ajax({
            url : "http://example.com/ajaxRequest/index.php",
            method : "POST",
            cache: false,
            data : {
                name : "Mohsin",
            },
            success : function(data){
                alert(data);         
            },
        });

相同的URL在浏览器中打开,但是ajax请求给出404错误.我也包括了

Same URL opens in browser but gives 404 Error with ajax Request. i have also included

header('Access-Control-Allow-Origin:*');

header('Access-Control-Allow-Origin: *');

在上面的脚本中

推荐答案

提及作为ajax中的跨域请求.还将jsonp用于跨域请求.

Mention as cross domain request in your ajax. And also use jsonp for cross domain requests.

url : "http://example.com/ajaxRequest/index.php",
        method : "POST",
        cache: false,
        data : {
            name : "Mohsin",
        },
        crossDomain:true,    <---- add this line
         dataType : 'jsonp',   // also this line
        success : function(data){
            alert(data);         
        },

jsonp 此处 查看全文

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