jQuery ajax()函数返回奇怪的响应

时间:2015-04-12 18:47:27

标签: javascript jquery ajax

我正在使用jQuery ajax()函数进行ajax调用。 我一直收到错误:

  

SyntaxError:意外的令牌<

这是我的功能:

function testAjax(subject)
{    
$.ajax({
        url: '/profile/testAjax',
        type: 'GET',
        data: { field1: subject} ,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (response) {
        //your success code
        alert(response);
        },
        error: function (xhr, ajaxOptions, thrownError) {
        alert("Error: " + thrownError);
     }
    }); 
}

在我的控制器中,testAjax功能如下:

public function testAjax($subject)
{
    return "This works:" . $subject;
}

此处调用该函数:

$(document).on('click', '#applyButton', function(e) 
{
e.preventDefault();
testAjax($(this).val()) 
});

任何人都知道我为什么会这样做?我知道这可能与contentTypedataType

有关

修改

正如评论所建议的,已经查看了Firebug中的HTTP响应:

GET write

200 OK

localhost

11.0 KB

127.0.0.1:80


83ms
GET jquery.min.js

200 OK

localhost

147 B

127.0.0.1:80


80ms
GET icons.woff2

200 OK

localhost

147 B

127.0.0.1:80


78ms
GET ajax?field1=Boogie

200 OK

localhost

3.1 KB

127.0.0.1:80


84ms
ParamsHeadersResponseHTMLCacheCookies

<br />
<font size='1'><table class='xdebug-error xe-warning' dir='ltr' border='1' cellspacing='0' cellpadding='1'>
<tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )</span> Warning: Missing argument 1 for profileController::ajax(), called in C:\wamp\www\application\router.class.php on line 85 and defined in C:\wamp\www\controller\profileController.php on line <i>440</i></th></tr>
<tr><th align='left' bgcolor='#e9b96e' colspan='5'>Call Stack</th></tr>
<tr><th align='center' bgcolor='#eeeeec'>#</th><th align='left' bgcolor='#eeeeec'>Time</th><th align='left' bgcolor='#eeeeec'>Memory</th><th align='left' bgcolor='#eeeeec'>Function</th><th align='left' bgcolor='#eeeeec'>Location</th></tr>
<tr><td bgcolor='#eeeeec' align='center'>1</td><td bgcolor='#eeeeec' align='center'>0.0006</td><td bgcolor='#eeeeec' align='right'>254992</td><td bgcolor='#eeeeec'>{main}(  )</td><td title='C:\wamp\www\index.php' bgcolor='#eeeeec'>..\index.php<b>:</b>0</td></tr>
<tr><td bgcolor='#eeeeec' align='center'>2</td><td bgcolor='#eeeeec' align='center'>0.0735</td><td bgcolor='#eeeeec' align='right'>2684528</td><td bgcolor='#eeeeec'>router->loader(  )</td><td title='C:\wamp\www\index.php' bgcolor='#eeeeec'>..\index.php<b>:</b>34</td></tr>
<tr><td bgcolor='#eeeeec' align='center'>3</td><td bgcolor='#eeeeec' align='center'>0.0755</td><td bgcolor='#eeeeec' align='right'>2766056</td><td bgcolor='#eeeeec'>profileController->ajax(  )</td><td title='C:\wamp\www\application\router.class.php' bgcolor='#eeeeec'>..\router.class.php<b>:</b>85</td></tr>
</table></font>
<br />
<font size='1'><table class='xdebug-error xe-notice' dir='ltr' border='1' cellspacing='0' cellpadding='1'>
<tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )</span> Notice: Undefined variable: subject in C:\wamp\www\controller\profileController.php on line <i>442</i></th></tr>
<tr><th align='left' bgcolor='#e9b96e' colspan='5'>Call Stack</th></tr>
<tr><th align='center' bgcolor='#eeeeec'>#</th><th align='left' bgcolor='#eeeeec'>Time</th><th align='left' bgcolor='#eeeeec'>Memory</th><th align='left' bgcolor='#eeeeec'>Function</th><th align='left' bgcolor='#eeeeec'>Location</th></tr>
<tr><td bgcolor='#eeeeec' align='center'>1</td><td bgcolor='#eeeeec' align='center'>0.0006</td><td bgcolor='#eeeeec' align='right'>254992</td><td bgcolor='#eeeeec'>{main}(  )</td><td title='C:\wamp\www\index.php' bgcolor='#eeeeec'>..\index.php<b>:</b>0</td></tr>
<tr><td bgcolor='#eeeeec' align='center'>2</td><td bgcolor='#eeeeec' align='center'>0.0735</td><td bgcolor='#eeeeec' align='right'>2684528</td><td bgcolor='#eeeeec'>router->loader(  )</td><td title='C:\wamp\www\index.php' bgcolor='#eeeeec'>..\index.php<b>:</b>34</td></tr>
<tr><td bgcolor='#eeeeec' align='center'>3</td><td bgcolor='#eeeeec' align='center'>0.0755</td><td bgcolor='#eeeeec' align='right'>2766056</td><td bgcolor='#eeeeec'>profileController->ajax(  )</td><td title='C:\wamp\www\application\router.class.php' bgcolor='#eeeeec'>..\router.class.php<b>:</b>85</td></tr>
</table></font>

0 个答案:

没有答案