发送ajax POST请求到restful api

I create an php slim framework based API by tutorial an now i'm able to register,login user with autentification:

URL     /register
Method  POST
Params  name, email, password

/register call dont need autentification so I write ajax to test API:

<button id="createPin"></button>
<script>
$(function() { 
    $('#createPin').click(function(e) { 
        e.preventDefault();


        var dataTest = { "name": "test", "email": "pepe@peperoni.com", "password": "sarasa" }
var urlAjax = "http://www.agroagro.com/test/v1/register";

$.ajax({
type: "POST",
url: urlAjax,
contentType: "application/json",
data: dataTest ,
success: function(data) { alert("ajax worked"); },
error: function(data) {console.log(data); },
dataType: 'json',
beforeSend: function (xhr) {
            xhr.setRequestHeader("Access-Control-Allow-Origin", "*");
        },
        headers: {
            'Access-Control-Allow-Origin': '*'
        }
});


}); 
});
</script>

I try to run this ajax call from localhost to agroagro.com/test/v1/register but I get:

XMLHttpRequest cannot load http://www.agroagro.com/test/v1/register. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access. The response had HTTP status code 404.

Is there any solution to make cross-domain ajax request becouse I have a plan to use this code as frontend of an mobile app, so I need to make requests via ajax cross-domain...

(restApi was created by following this tutorial:http://www.androidhive.info/2014/01/how-to-create-rest-api-for-android-app-using-php-slim-and-mysql-day-23/)

查看全部
duanqin4238
duanqin4238
2014/11/27 15:17
  • rest
  • cross-domain
  • php
  • ajax
  • jquery
  • 点赞
  • 收藏
  • 回答
    私信
满意答案
查看全部

1个回复