创建一个ajax跨域请求来休息laravel api

时间:2014-11-27 00:08:23

标签: php ajax cordova laravel cross-domain

我创建了一个带有phonegap的移动应用程序,我不知道它有多聪明,但我想用户注册。在我的应用程序上,他们的电话号码和密码。用户将下载应用程序,键入电话号码并通过短信(twillio api)获取PIN ...现在我有一个基本问题,如何运行带有ajax跨域的laravel php代码。

我做:

HTML + JS + AJAX:

<body class='bg-blue'>


    </br>
    <h1 class="text-center">AgroAgro</h1>
    </br>
    <h4 class="text-center">Register manager account and add workers.</h4>
    </br>
    <div class="col-xs-12"><p>Your Phone Number</p></div>
    <div class="col-xs-12">
        <input class="form-control" placeholder="Your Phone Number"/>
    </div>


    <div class="col-xs-12"></br></div>

        <div class="col-xs-12 text-center">
            <button id="createPin" class="btn btn-success btn-lg">FREE sign up</button>
        </div>
<footer>We will send you a PIN code via text message immediately to login and get started. We will never share or span your phone number.</footer>


<script>
$(function() { 
    $('#createPin').click(function(e) { 
        e.preventDefault();


        $.ajax({
            url: "localhost:8000/createPin",
            type: "POST",
            async: true, 
            data: { phoneNumber:$("#createPin").val()},
            dataType: "json",

            success: function(data) {
                console.log(data); 
                //do something
            }, 
            error: function (data) {
            console.log(data);
            } 
        });


}); 
});
</script>

在后端,我用laravel框架编写php:

Route::put('createPin', function()
{
    $phoneNumber = Input::get('phoneNumber');
    $pin = rand(1000,9999);
    $user = new User;


$user->phoneNumber = $phoneNumber;
$user->pin = $pin;

// this line loads the library 
require('/path/to/twilio-php/Services/Twilio.php'); 

$account_sid = 'AC6333333911376fghhbced190522da587'; 
$auth_token = '[AuthToken]'; 
$client = new Services_Twilio($account_sid, $auth_token); 

$client->account->messages->create(array(  
    'To' => $phoneNumber, 
    'From' => "+18702802244",  
    'Body' => "PIN code", 
    'MediaUrl' => "Your pin code is ".$pin,   
));

$user->save();

    return $users;
});

当我点击带有IDcreatPin的按钮时,我收到此错误:

XMLHttpRequest cannot load localhost:8000/createPin. Cross origin requests are only supported for protocol schemes: http, data, chrome-extension, https, chrome-extension-resource.

我如何解决这个问题?你还想到了PIN而不是密码的想法吗?

0 个答案:

没有答案