Slim Framework v3 - PUT路由问题

时间:2016-02-04 11:41:58

标签: javascript php google-chrome slim

由于我已升级到Slim v3,所有GETPOST路由都运行良好,但PUT路由不正常。 我有一个班级,我做这样的事情:

$this->slimObj->put('/path/{ID}', array($this, 'method'));
function method ( $request, $response, $args ) { 
    $response = $response->withHeader('Content-type', 'application/json');
    $response = $response->withHeader('Access-Control-Allow-Origin', '*');
    $ID = $args['ID'];
    // ...
    return $response; 
}

我使用Chrome 48调试我的Cordova应用,我在PUT电话后收到此错误:

XMLHttpRequest cannot load 
http://example.com/file.php/path/149. No 'Access-Control-Allow-Origin' header is present on the requested resource. 
Origin 'http://localhost:4400' is therefore not allowed access. The response had HTTP status code 400.

我对GETPOST请求使用类似的回调,它们工作正常,我不明白为什么它对PUT不起作用。

curl调用它时,它可以工作:

curl -X PUT -H 'Content-Type: application/json' -d '{"data": 5 }' http://example.com/file.php/path/149

我使用JS

ajax调用Slim API函数
$.ajax({
    type: 'PUT',
    url: 'http://example.com/file.php/path/149',
    dataType: "json", 
    data:  {
       "data": 5
    },
    success: function(result) {
        // ...
    },
    error: function() {
        // Always getting error
    }
});

如何从Chrome 48开始使用它?

我不太确定,但我认为以前的Chrome版本一切正常。

由于

1 个答案:

答案 0 :(得分:0)

我刚刚解决了这个问题:

SqlCommand cmd1 = new SqlCommand("spG_Groups", conn);
cmd1.CommandType = CommandType.StoredProcedure;

List<String> YrStrList1 = new List<string>(); 

foreach (ListItem li in chGp.Items)
{
    if (li.Selected)
    {
        YrStrList1.Add(li.Value);

        cmd1.Parameters.Add(new SqlParameter("@CName", txtCName.Value));
        cmd1.Parameters.Add(new SqlParameter("@CEmail", txtemail.Value));
        cmd1.Parameters.Add(new SqlParameter("@GName", YrStrList1.ToString()));

        cmd1.ExecuteNonQuery();
    }
}

在初始化任何路线之前。